1<?php
2
3namespace OAuth\OAuth2\Service;
4
5use OAuth\OAuth2\Token\StdOAuth2Token;
6use OAuth\Common\Http\Exception\TokenResponseException;
7use OAuth\Common\Http\Uri\Uri;
8use OAuth\Common\Consumer\CredentialsInterface;
9use OAuth\Common\Http\Client\ClientInterface;
10use OAuth\Common\Storage\TokenStorageInterface;
11use OAuth\Common\Http\Uri\UriInterface;
12
13class Instagram extends AbstractService
14{
15    /**
16     * Defined scopes
17     * @link http://instagram.com/developer/authentication/#scope
18     */
19    const SCOPE_BASIC         = 'basic';
20    const SCOPE_PUBLIC_CONTENT = 'public_content';
21    const SCOPE_COMMENTS      = 'comments';
22    const SCOPE_RELATIONSHIPS = 'relationships';
23    const SCOPE_LIKES         = 'likes';
24    const SCOPE_FOLLOWER_LIST = 'follower_list';
25
26    public function __construct(
27        CredentialsInterface $credentials,
28        ClientInterface $httpClient,
29        TokenStorageInterface $storage,
30        $scopes = array(),
31        UriInterface $baseApiUri = null
32    ) {
33        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
34
35        if (null === $baseApiUri) {
36            $this->baseApiUri = new Uri('https://api.instagram.com/v1/');
37        }
38    }
39
40    /**
41     * {@inheritdoc}
42     */
43    public function getAuthorizationEndpoint()
44    {
45        return new Uri('https://api.instagram.com/oauth/authorize/');
46    }
47
48    /**
49     * {@inheritdoc}
50     */
51    public function getAccessTokenEndpoint()
52    {
53        return new Uri('https://api.instagram.com/oauth/access_token');
54    }
55
56    /**
57     * {@inheritdoc}
58     */
59    protected function getAuthorizationMethod()
60    {
61        return static::AUTHORIZATION_METHOD_QUERY_STRING;
62    }
63
64    /**
65     * {@inheritdoc}
66     */
67    protected function parseAccessTokenResponse($responseBody)
68    {
69        $data = json_decode($responseBody, true);
70
71        if (null === $data || !is_array($data)) {
72            throw new TokenResponseException('Unable to parse response.');
73        } elseif (isset($data['error'])) {
74            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
75        }
76
77        $token = new StdOAuth2Token();
78        $token->setAccessToken($data['access_token']);
79        // Instagram tokens evidently never expire...
80        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
81        unset($data['access_token']);
82
83        $token->setExtraParams($data);
84
85        return $token;
86    }
87}
88