-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e2c46df
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Instagram Provider | ||
|
||
## Installation | ||
|
||
``` | ||
composer require socialite-manager/instagram-provider | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
use Socialite\Provider\InstagramProvider; | ||
use Socialite\Socialite; | ||
|
||
Socialite::driver(InstagramProvider::class, $config); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "socialite-manager/instagram-provider", | ||
"description": "instagram provider", | ||
"license": "MIT", | ||
"require": { | ||
"socialite-manager/socialite": "^1.0" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^0.9.2", | ||
"squizlabs/php_codesniffer": "^3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Socialite\\Provider\\": "src/" | ||
} | ||
}, | ||
"scripts": { | ||
"check": [ | ||
"@cs-check", | ||
"@analyse" | ||
], | ||
"cs-check": "phpcs --standard=PSR2 --colors -p ./src", | ||
"cs-fix": "phpcbf --standard=PSR2 --colors ./src", | ||
"analyse": "phpstan analyse -l max src" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Socialite\Provider; | ||
|
||
use Socialite\Two\AbstractProvider; | ||
use Socialite\Two\User; | ||
|
||
class InstagramProvider extends AbstractProvider | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopeSeparator = ' '; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopes = ['basic']; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl(string $state) | ||
{ | ||
return $this->buildAuthUrlFromBase( | ||
'https://api.instagram.com/oauth/authorize', | ||
$state | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return 'https://api.instagram.com/oauth/access_token'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken(string $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', | ||
[ | ||
'query' => $query, | ||
'headers' => [ | ||
'Accept' => 'application/json', | ||
], | ||
] | ||
); | ||
|
||
return json_decode($response->getBody()->getContents(), true)['data']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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'], | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenFields(string $code) | ||
{ | ||
return array_merge(parent::getTokenFields($code), [ | ||
'grant_type' => 'authorization_code', | ||
]); | ||
} | ||
|
||
/** | ||
* Allows compatibility for signed API requests. | ||
* | ||
* @param string $endpoint | ||
* @param array $params | ||
* @return string | ||
*/ | ||
protected function generateSignature(string $endpoint, array $params) | ||
{ | ||
$sig = $endpoint; | ||
ksort($params); | ||
foreach ($params as $key => $val) { | ||
$sig .= "|$key=$val"; | ||
} | ||
$signing_key = $this->clientSecret; | ||
return hash_hmac('sha256', $sig, $signing_key, false); | ||
} | ||
} |