5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
14 * Handles authentication
16 * {@link https://github.com/galen/PHP-Instagram-API#authentication}
17 * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/_auth.php}
24 * Contains a default client and proxy
26 * client_id: These three items are required for authorization
27 * redirect_uri: URL that the Instagram API shoudl redirect to
28 * grant_type: Grant type from the Instagram API. Only authorization_code is accepted right now.
29 * scope: {@link http://instagram.com/developer/auth/#scope}
30 * display: Pass in "touch" if you'd like your authenticating users to see a mobile-optimized
31 * version of the authenticate page and the sign-in page.
36 protected $config = array(
38 'client_secret' => '',
40 'grant_type' => 'authorization_code',
41 'scope' => array( 'basic' ),
48 * @param array $config Configuration array
49 * @param \Instagram\Net\ClientInterface $client Client object used to connect to the API
52 public function __construct( array $config = null, \Instagram\Net\ClientInterface $client = null ) {
53 $this->config = (array) $config + $this->config;
54 $this->proxy = new \Instagram\Core\Proxy( $client ? $client : new \Instagram\Net\CurlClient );
60 * Redirects the user to the Instagram authorization url
63 public function authorize() {
66 'Location:https://api.instagram.com/oauth/authorize/?client_id=%s&redirect_uri=%s&response_type=code&scope=%s',
67 $this->config['client_id'],
68 $this->config['redirect_uri'],
69 implode( '+', $this->config['scope'] )
76 * Get the access token
78 * POSTs to the Instagram API and obtains and access key
80 * @param string $code Code supplied by Instagram
81 * @return string Returns the access token
82 * @throws \Instagram\Core\ApiException
85 public function getAccessToken( $code ) {
87 'client_id' => $this->config['client_id'],
88 'client_secret' => $this->config['client_secret'],
89 'grant_type' => $this->config['grant_type'],
90 'redirect_uri' => $this->config['redirect_uri'],
93 $response = $this->proxy->getAccessToken( $post_data );
94 if ( isset( $response->getRawData()->access_token ) ) {
95 return $response->getRawData()->access_token;
97 throw new \Instagram\Core\ApiException( $response->getErrorMessage(), $response->getErrorCode(), $response->getErrorType() );