Skip to content

Commit

Permalink
Update Instagram provider to match Instagram api and work again. (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykenedy authored Jan 3, 2023
1 parent f969160 commit c3ef6b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 43 deletions.
67 changes: 27 additions & 40 deletions Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@

class Provider extends AbstractProvider
{
/**
* Unique Provider Identifier.
*/
public const IDENTIFIER = 'INSTAGRAM';

/**
* {@inheritdoc}
*/
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}
Expand All @@ -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);
}

/**
Expand All @@ -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,
]);
}

Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ return Socialite::driver('instagram')->redirect();
### Returned User fields

- ``id``
- ``nickname``
- ``name``
- ``avatar``
- ``username``
- ``account_type``
- ``media_count``

0 comments on commit c3ef6b6

Please sign in to comment.