Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mosaxiv committed Mar 18, 2018
0 parents commit e2c46df
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor/
composer.lock
16 changes: 16 additions & 0 deletions README.md
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);
```
26 changes: 26 additions & 0 deletions composer.json
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"
}
}
104 changes: 104 additions & 0 deletions src/InstagramProvider.php
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);
}
}

0 comments on commit e2c46df

Please sign in to comment.