diff --git a/Provider.php b/Provider.php index fc7d3f2..8d7b585 100644 --- a/Provider.php +++ b/Provider.php @@ -8,6 +8,9 @@ class Provider extends AbstractProvider { + /** + * Unique Provider Identifier. + */ public const IDENTIFIER = 'INSTAGRAM'; /** @@ -15,10 +18,17 @@ class Provider extends AbstractProvider */ protected $scopeSeparator = ' '; + /** + * The user fields being requested. + * + * @var array + */ + protected $fields = ['account_type', 'id', 'username', 'media_count']; + /** * {@inheritdoc} */ - protected $scopes = ['basic']; + protected $scopes = ['user_profile']; /** * {@inheritdoc} @@ -44,24 +54,19 @@ protected function getTokenUrl() */ protected function getUserByToken($token) { - $endpoint = '/users/self'; - $query = [ - 'access_token' => $token, - ]; - $signature = $this->generateSignature($endpoint, $query); - - $query['sig'] = $signature; - $response = $this->getHttpClient()->get( - 'https://api.instagram.com/v1/users/self', - [ - RequestOptions::QUERY => $query, - RequestOptions::HEADERS => [ - 'Accept' => 'application/json', - ], - ] - ); + $meUrl = 'https://graph.instagram.com/me?access_token='.$token.'&fields='.implode(',', $this->fields); + + if (!empty($this->clientSecret)) { + $appSecretProof = hash_hmac('sha256', $token, $this->clientSecret); + $meUrl .= '&appsecret_proof='.$appSecretProof; + } + $response = $this->getHttpClient()->get($meUrl, [ + RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + ], + ]); - return json_decode((string) $response->getBody(), true)['data']; + return json_decode($response->getBody(), true); } /** @@ -70,9 +75,10 @@ protected function getUserByToken($token) protected function mapUserToObject(array $user) { return (new User())->setRaw($user)->map([ - 'id' => $user['id'], 'nickname' => $user['username'], - 'name' => $user['full_name'], 'email' => null, - 'avatar' => $user['profile_picture'], + 'id' => $user['id'], + 'name' => $user['username'], + 'account_type' => $user['account_type'], + 'media_count' => $user['media_count'] ?? null, ]); } @@ -96,23 +102,4 @@ protected function getTokenFields($code) 'grant_type' => 'authorization_code', ]); } - - /** - * Allows compatibility for signed API requests. - * - * @param string @endpoint - * @param array $params - * - * @return string - */ - protected function generateSignature($endpoint, array $params) - { - $sig = $endpoint; - ksort($params); - foreach ($params as $key => $val) { - $sig .= "|$key=$val"; - } - - return hash_hmac('sha256', $sig, $this->clientSecret, false); - } } diff --git a/README.md b/README.md index 5e39e6f..fa2c6bc 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,6 @@ return Socialite::driver('instagram')->redirect(); ### Returned User fields - ``id`` -- ``nickname`` -- ``name`` -- ``avatar`` +- ``username`` +- ``account_type`` +- ``media_count``