Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Auth.php
1 <?php
2
3 /**
4  * Instagram PHP
5  * @author Galen Grover <galenjr@gmail.com>
6  * @license http://opensource.org/licenses/mit-license.php The MIT License
7  */
8
9 namespace Instagram;
10
11 /**
12  * Auth class
13  *
14  * Handles authentication
15  * 
16  * {@link https://github.com/galen/PHP-Instagram-API#authentication}
17  * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/_auth.php}
18  */
19 class Auth {
20
21     /**
22      * Configuration array
23      *
24      * Contains a default client and proxy
25      *
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. 
32      *
33      * @var array
34      * @access protected
35      */
36         protected $config = array(
37         'client_id'     => '',
38         'client_secret' => '',
39         'redirect_uri'  => '',
40         'grant_type'    => 'authorization_code',
41         'scope'         => array( 'basic' ),
42         'display'       => ''
43         );
44
45     /**
46      * Constructor
47      *
48      * @param array $config Configuration array
49      * @param \Instagram\Net\ClientInterface $client Client object used to connect to the API
50      * @access public
51      */
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 );
55     }
56
57     /**
58      * Authorize
59      *
60      * Redirects the user to the Instagram authorization url
61      * @access public
62      */
63     public function authorize() {
64         header(
65             sprintf(
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'] )
70             )
71         );
72         exit;
73     }
74
75     /**
76      * Get the access token
77      *
78      * POSTs to the Instagram API and obtains and access key
79      *
80      * @param string $code Code supplied by Instagram
81      * @return string Returns the access token
82      * @throws \Instagram\Core\ApiException
83      * @access public
84      */
85     public function getAccessToken( $code ) {
86         $post_data = array(
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'],
91             'code'              => $code
92         );
93         $response = $this->proxy->getAccessToken( $post_data );
94         if ( isset( $response->getRawData()->access_token ) ) {
95             return $response->getRawData()->access_token;
96         }
97         throw new \Instagram\Core\ApiException( $response->getErrorMessage(), $response->getErrorCode(), $response->getErrorType() );
98     }
99
100
101 }