source: extensions/instagram2piwigo/include/Instagram/Auth.php @ 19561

Last change on this file since 19561 was 19561, checked in by mistic100, 11 years ago

first commit

File size: 3.5 KB
RevLine 
[19561]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
9defined('INSTAGRAM_ROOT') or define('INSTAGRAM_ROOT', realpath(dirname(__FILE__)));
10 
11include_once(INSTAGRAM_ROOT.'/Core/Proxy.php');
12include_once(INSTAGRAM_ROOT.'/Core/ApiException.php');
13include_once(INSTAGRAM_ROOT.'/Net/ClientInterface.php');
14include_once(INSTAGRAM_ROOT.'/Net/CurlClient.php');
15
16
17/**
18 * Auth class
19 *
20 * Handles authentication
21 *
22 * {@link https://github.com/galen/PHP-Instagram-API#authentication}
23 * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/_auth.php}
24 */
25class Instagram_Auth {
26
27    /**
28     * Configuration array
29     *
30     * Contains a default client and proxy
31     *
32     * client_id:       These three items are required for authorization
33     * redirect_uri:    URL that the Instagram API shoudl redirect to
34     * grant_type:      Grant type from the Instagram API. Only authorization_code is accepted right now.
35     * scope:           {@link http://instagram.com/developer/auth/#scope}
36     * display:         Pass in "touch" if you'd like your authenticating users to see a mobile-optimized
37     *                  version of the authenticate page and the sign-in page.
38     *
39     * @var array
40     * @access protected
41     */
42        protected $config = array(
43        'client_id'     => '',
44        'client_secret' => '',
45        'redirect_uri'  => '',
46        'grant_type'    => 'authorization_code',
47        'scope'         => array( 'basic' ),
48        'display'       => ''
49        );
50
51    /**
52     * Constructor
53     *
54     * @param array $config Configuration array
55     * @param Instagram_Net_ClientInterface $client Client object used to connect to the API
56     * @access public
57     */
58    public function __construct( array $config = null, Instagram_Net_ClientInterface $client = null ) {
59        $this->config = (array) $config + $this->config;
60        $this->proxy = new Instagram_Core_Proxy( $client ? $client : new Instagram_Net_CurlClient );
61    }
62
63    /**
64     * Authorize
65     *
66     * Redirects the user to the Instagram authorization url
67     * @access public
68     */
69    public function authorize() {
70        header(
71            sprintf(
72                'Location:https://api.instagram.com/oauth/authorize/?client_id=%s&redirect_uri=%s&response_type=code&scope=%s',
73                $this->config['client_id'],
74                $this->config['redirect_uri'],
75                implode( '+', $this->config['scope'] )
76            )
77        );
78        exit;
79    }
80
81    /**
82     * Get the access token
83     *
84     * POSTs to the Instagram API and obtains and access key
85     *
86     * @param string $code Code supplied by Instagram
87     * @return string Returns the access token
88     * @throws Instagram_Core_ApiException
89     * @access public
90     */
91    public function getAccessToken( $code ) {
92        $post_data = array(
93            'client_id'         => $this->config['client_id'],
94            'client_secret'     => $this->config['client_secret'],
95            'grant_type'        => $this->config['grant_type'],
96            'redirect_uri'      => $this->config['redirect_uri'],
97            'code'              => $code
98        );
99        $response = $this->proxy->getAccessToken( $post_data );
100        if ( isset( $response->getRawData()->access_token ) ) {
101            return $response->getRawData()->access_token;
102        }
103        throw new Instagram_Core_ApiException( $response->getErrorMessage(), $response->getErrorCode(), $response->getErrorType() );
104    }
105
106
107}
108?>
Note: See TracBrowser for help on using the repository browser.