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
+ ?>
0 commit comments