Skip to content

Commit 4511c15

Browse files
committed
adding ability to get access tokens
1 parent 4407700 commit 4511c15

38 files changed

+576
-70
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Justin Stolpe.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
namespace Instagram\AccessToken;
25+
26+
// other classes we need to use
27+
use Instagram\Instagram;
28+
use Instagram\Request\Params;
29+
use Instagram\Request\GrantTypes;
30+
31+
/**
32+
* AccessToken
33+
*
34+
* Core functionality for access tokens.
35+
* - Endpoint Format: GET /oauth/access_token?client_id={your-app-id}&client_secret={your-app-secret}&redirect_uri={redirect-uri}&code={code-parameter}
36+
* - Endpoint Format: GET /oauth/access_token?client_id={your-app-id}&client_secret={your-app-secret}&grant_type=fb_exchange_token&fb_exchange_token={access-token}
37+
* - Endpoint Format: GET /debug_token?input_token={access-token}&access_token={access-token}
38+
* - Endpoint Format: GET|DELETE /{ig-user-id}/permissions/{permission-name}?access_token={access-token}
39+
* - Facebook Docs: https://developers.facebook.com/docs/facebook-login/guides/access-tokens
40+
*
41+
* @package instagram-graph-api-php-sdk
42+
* @author Justin Stolpe
43+
* @link https://github.com/jstolpe/instagram-graph-api-php-sdk
44+
* @license https://opensource.org/licenses/MIT
45+
* @version 1.0
46+
*/
47+
class AccessToken extends Instagram {
48+
/**
49+
* @const debug token endpoint
50+
*/
51+
const ENDPOINT_DEBUG = 'debug_token';
52+
53+
/**
54+
* @const token permissions endpoint
55+
*/
56+
const ENDPOINT_PERMISSIONS = 'permissions';
57+
58+
/**
59+
* @const access token endpoint.
60+
*/
61+
const ENDPOINT_TOKEN = 'oauth/access_token';
62+
63+
/**
64+
* @var integer $appId Facebook application id.
65+
*/
66+
protected $appId;
67+
68+
/**
69+
* @var integer $appId Facebook application secret.
70+
*/
71+
protected $appSecret;
72+
73+
/**
74+
* @var integer $expiresAt time when the access token expires.
75+
*/
76+
protected $expiresAt = 0;
77+
78+
/**
79+
* @var string $userId the user id of the access token.
80+
*/
81+
protected $userId;
82+
83+
/**
84+
* @var string $value the access token.
85+
*/
86+
protected $value;
87+
88+
/**
89+
* Contructor for instantiating a new object.
90+
*
91+
* @param array $config for the class.
92+
* @return void
93+
*/
94+
public function __construct( $config = array() ) {
95+
// call parent for setup
96+
parent::__construct( $config );
97+
98+
// set the application id
99+
$this->appId = isset( $config['app_id'] ) ? $config['app_id'] : '';
100+
101+
// set the application secret
102+
$this->appSecret = isset( $config['app_secret'] ) ? $config['app_secret'] : '';
103+
104+
// set the access token
105+
$this->value = isset( $config['value'] ) ? $config['value'] : '';
106+
107+
// set the access token expire date
108+
$this->expiresAt = isset( $config['expires_at'] ) ? $config['expires_at'] : $this->expiresAt;
109+
110+
// set the user id
111+
$this->userId = isset( $config['user_id'] ) ? $config['user_id'] : '';
112+
}
113+
114+
/**
115+
* Debug the access token.
116+
*
117+
* @return Instagram response.
118+
*/
119+
public function debug() {
120+
$getParams = array( // parameters for our endpoint
121+
'endpoint' => '/' . self::ENDPOINT_DEBUG,
122+
'params' => array(
123+
Params::INPUT_TOKEN => $this->value
124+
)
125+
);
126+
127+
// get request
128+
$response = $this->get( $getParams );
129+
130+
// return response
131+
return $response;
132+
}
133+
134+
/**
135+
* Get access token from a code.
136+
*
137+
* @param string $code the code from the facebook redirect.
138+
* @param string $redirectUri uri the user gets sent to after logging in with facebook.
139+
* @return Instagram response.
140+
*/
141+
public function getAccessTokenFromCode( $code, $redirectUri ) {
142+
$getParams = array( // parameters for our endpoint
143+
'endpoint' => '/' . self::ENDPOINT_TOKEN,
144+
'params' => array(
145+
Params::CLIENT_ID => $this->appId,
146+
Params::CLIENT_SECRET => $this->appSecret,
147+
Params::REDIRECT_URI => $redirectUri,
148+
Params::CODE => $code
149+
)
150+
);
151+
152+
// get request
153+
$response = $this->get( $getParams );
154+
155+
// set class vars from response data
156+
$this->setDataFromResponse( $response );
157+
158+
// return response
159+
return $response;
160+
}
161+
162+
/**
163+
* Get a long lived access token.
164+
*
165+
* @param string $accessToken the access token to exchange for a long lived one.
166+
* @return Instagram response.
167+
*/
168+
public function getLongLivedAccessToken( $accessToken ) {
169+
$getParams = array( // parameters for our endpoint
170+
'endpoint' => '/' . self::ENDPOINT_TOKEN,
171+
'params' => array(
172+
Params::CLIENT_ID => $this->appId,
173+
Params::CLIENT_SECRET => $this->appSecret,
174+
Params::FB_EXCHANGE_TOKEN => $accessToken,
175+
Params::GRANT_TYPE => GrantTypes::FB_EXCHANGE_TOKEN,
176+
177+
)
178+
);
179+
180+
// get request
181+
$response = $this->get( $getParams );
182+
183+
// set class vars from response data
184+
$this->setDataFromResponse( $response );
185+
186+
// return response
187+
return $response;
188+
}
189+
190+
/**
191+
* Get the permissions a user has for the access token.
192+
*
193+
* @return Instagram response.
194+
*/
195+
public function getPermissions() {
196+
$getParams = array( // parameters for our endpoint
197+
'endpoint' => '/' . $this->userId. '/' . self::ENDPOINT_PERMISSIONS
198+
);
199+
200+
// get request
201+
$response = $this->get( $getParams );
202+
203+
// return response
204+
return $response;
205+
}
206+
207+
/**
208+
* Check if the access token is long lived or not.
209+
*
210+
* @return boolean
211+
*/
212+
public function isLongLived() {
213+
// check if expires at meets long lived requirements
214+
return $this->expiresAt ? $this->expiresAt > time() + ( 60 * 60 * 2 ) : true;
215+
}
216+
217+
/**
218+
* Revoke permissions for a users access token.
219+
*
220+
* @param string $permissionName name of the permission to revoke leave blank to revoke all permissions.
221+
* @return Instagram response.
222+
*/
223+
public function revokePermissions( $permissionName = '' ) {
224+
$deleteParams = array( // parameters for our endpoint
225+
'endpoint' => '/' . $this->userId. '/' . self::ENDPOINT_PERMISSIONS . '/' . $permissionName
226+
);
227+
228+
// delete request
229+
$response = $this->delete( $deleteParams );
230+
231+
// return response
232+
return $response;
233+
}
234+
235+
/**
236+
* Set class variables from the token response.
237+
*
238+
* @param array $response the response from the api call.
239+
* @return void
240+
*/
241+
public function setDataFromResponse( $response ) {
242+
if ( isset( $response['access_token'] ) ) { // we have an access token
243+
// set the value from the response
244+
$this->value = $response['access_token'];
245+
246+
// set expires at if we have it in the response
247+
$this->expiresAt = isset( $response['expires_in'] ) ? time() + $response['expires_in'] : 0;
248+
}
249+
}
250+
}
251+
252+
?>

src/Instagram/Comment/Comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
* - Endpoint Format: DELETE /{ig-comment-id}?access_token={access-token}
3838
* - Facebook docs: https://developers.facebook.com/docs/instagram-api/reference/ig-comment
3939
*
40-
* @package instagram-graph-api
40+
* @package instagram-graph-api-php-sdk
4141
* @author Justin Stolpe
42-
* @link https://github.com/jstolpe/instagram-graph-api
42+
* @link https://github.com/jstolpe/instagram-graph-api-php-sdk
4343
* @license https://opensource.org/licenses/MIT
4444
* @version 1.0
4545
*/

src/Instagram/Comment/Replies.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
* - Endpoint Format: POST /{ig-comment-id}/replies?message={message}&access_token={access-token}
3737
* - Facebook docs: https://developers.facebook.com/docs/instagram-api/reference/ig-comment/replies
3838
*
39-
* @package instagram-graph-api
39+
* @package instagram-graph-api-php-sdk
4040
* @author Justin Stolpe
41-
* @link https://github.com/jstolpe/instagram-graph-api
41+
* @link https://github.com/jstolpe/instagram-graph-api-php-sdk
4242
* @license https://opensource.org/licenses/MIT
4343
* @version 1.0
4444
*/

src/Instagram/Container/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
* - Endpoint Format: GET /{ig-container-id}/?fields={fields}&access_token={access-token}
3636
* - Facebook docs: https://developers.facebook.com/docs/instagram-api/reference/ig-container
3737
*
38-
* @package instagram-graph-api
38+
* @package instagram-graph-api-php-sdk
3939
* @author Justin Stolpe
40-
* @link https://github.com/jstolpe/instagram-graph-api
40+
* @link https://github.com/jstolpe/instagram-graph-api-php-sdk
4141
* @license https://opensource.org/licenses/MIT
4242
* @version 1.0
4343
*/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Justin Stolpe.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
namespace Instagram\FacebookLogin;
25+
26+
// other classes we need to use
27+
use Instagram\Instagram;
28+
use Instagram\Request\Request;
29+
use Instagram\Request\Params;
30+
use Instagram\Request\ResponseTypes;
31+
32+
/**
33+
* FacebookLogin
34+
*
35+
* Core functionality for login dialog.
36+
* - Facebook Docs: https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/
37+
*
38+
* @package instagram-graph-api-php-sdk
39+
* @author Justin Stolpe
40+
* @link https://github.com/jstolpe/instagram-graph-api-php-sdk
41+
* @license https://opensource.org/licenses/MIT
42+
* @version 1.0
43+
*/
44+
class FacebookLogin extends Instagram {
45+
/**
46+
* @const debug token endpoint
47+
*/
48+
const ENDPOINT = 'dialog/oauth';
49+
50+
/**
51+
* @var integer $appId Facebook application id.
52+
*/
53+
protected $appId;
54+
55+
/**
56+
* @var integer $appId Facebook application secret.
57+
*/
58+
protected $appSecret;
59+
60+
/**
61+
* Contructor for instantiating a new object.
62+
*
63+
* @param array $config for the class.
64+
* @return void
65+
*/
66+
public function __construct( $config = array() ) {
67+
// call parent for setup
68+
parent::__construct( $config );
69+
70+
// set the application id
71+
$this->appId = $config['app_id'];
72+
73+
// set the application secret
74+
$this->appSecret = $config['app_secret'];
75+
}
76+
77+
/**
78+
* Get the url for a user to prompt them with the login dialog.
79+
*
80+
* @param string $redirectUri uri the user gets sent to after logging in with facebook.
81+
* @param array $permissions array of the permissions you want to request from the user.
82+
* @param string $state this gets passed back from facebook in the redirect uri.
83+
* @return Instagram response.
84+
*/
85+
public function getLoginDialogUrl( $redirectUri, $permissions, $state = '' ) {
86+
$params = array( // params required to generate the login url
87+
Params::CLIENT_ID => $this->appId,
88+
Params::REDIRECT_URI => $redirectUri,
89+
Params::STATE => $state,
90+
Params::SCOPE => Params::commaImplodeArray( $permissions ),
91+
Params::RESPONSE_TYPE => ResponseTypes::CODE,
92+
);
93+
94+
// return the login dialog url
95+
return Request::BASE_AUTHORIZATION_URL . '/' . $this->graphVersion . '/' . self::ENDPOINT . '?' . http_build_query( $params );;
96+
}
97+
}
98+
99+
?>

0 commit comments

Comments
 (0)