Skip to content

Commit

Permalink
Merge pull request #3 from REZ1DENT3/feature
Browse files Browse the repository at this point in the history
patch twitter and add provider instagram
  • Loading branch information
dracony authored Oct 12, 2016
2 parents cd91eb0 + 4660204 commit f864cd8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/PHPixie/Social/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class Builder
protected $providers;

protected $providerClassMap = array(
'facebook'=> '\PHPixie\Social\Providers\Facebook\Provider',
'google' => '\PHPixie\Social\Providers\Google\Provider',
'twitter' => '\PHPixie\Social\Providers\Twitter\Provider',
'vk' => '\PHPixie\Social\Providers\VK\Provider',
'facebook' => Providers\Facebook\Provider::class,
'google' => Providers\Google\Provider::class,
'twitter' => Providers\Twitter\Provider::class,
'vk' => Providers\Vk\Provider::class,
'instagram' => Providers\Instagram\Provider::class
);

public function __construct($configData)
Expand Down
87 changes: 87 additions & 0 deletions src/PHPixie/Social/Providers/Instagram/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace PHPixie\Social\Providers\Instagram;

class Provider extends \PHPixie\Social\OAuth\OAuth2\Provider
{
protected $loginDataEndpoint = 'users/self';

protected function endpointUrl($endpoint)
{
$version = $this->configData->get('apiVersion', '1');

return 'https://api.instagram.com/v' . $version . '/' . $endpoint;
}

public function loginUrl($callbackUrl, $additionalScope = array())
{
$scope = array_merge(
$this->configData->get('scope', array()),
$additionalScope
);

return $this->buildLoginUrl(
'https://api.instagram.com/oauth/authorize/',
$callbackUrl,
array(
'scope' => implode(' ', $scope),
'response_type' => 'code'
)
);
}

public function handleCallback($callbackUrl, $callbackData)
{
if (!isset($callbackData['code']))
{
return null;
}

$baseParameters = array(
'client_id' => $this->configData->getRequired('appId'),
'client_secret' => $this->configData->getRequired('appSecret'),
'grant_type' => 'authorization_code',
'redirect_uri' => $callbackUrl,
'code' => $callbackData['code']
);

$tokenData = $this->getTokenResponse($callbackData, $baseParameters);
$tokenData = $this->decodeApiResponse($tokenData);

$loginData = $this->apiCall(
$tokenData->access_token,
'GET',
$this->loginDataEndpoint
);

$loginData = $this->normalizeLoginData($loginData, $tokenData);

$token = $this->token(
$this->getUserId($loginData),
$tokenData->access_token,
3600
);

return $this->user($token, $loginData);
}

protected function getUserId($loginData)
{
return $loginData->data->id;
}

protected function getTokenResponse($callbackData, $baseParameters)
{
return $this->http()->call(
'POST',
'https://api.instagram.com/oauth/access_token',
array(),
http_build_query($baseParameters)
);
}

public function type()
{
return 'instagram';
}
}
2 changes: 0 additions & 2 deletions src/PHPixie/Social/Providers/Twitter/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ protected function getAuthHeader($method, $url, $parameters = array(), $token, $

$data['oauth_signature'] = base64_encode(hash_hmac('sha1', $signData, $signKey, true));

$header = 'OAuth ';

$header = array();
foreach($data as $key => $value) {
$header[] = urlencode($key).'="'.urlencode($value).'"';
Expand Down

0 comments on commit f864cd8

Please sign in to comment.