Skip to content

Commit 372e6d7

Browse files
committed
instagram graph api access tokens with python
1 parent 311f0cf commit 372e6d7

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from defines import getCreds, makeApiCall
2+
import datetime
3+
4+
def debugAccessToken( params ) :
5+
""" Get info on an access token
6+
7+
API Endpoint:
8+
https://graph.facebook.com/debug_token?input_token={input-token}&access_token={valid-access-token}
9+
10+
Returns:
11+
object: data from the endpoint
12+
13+
"""
14+
15+
endpointParams = dict()
16+
endpointParams['input_token'] = params['access_token']
17+
endpointParams['access_token'] = params['access_token']
18+
19+
url = params['graph_domain'] + '/debug_token'
20+
21+
return makeApiCall( url, endpointParams, params['debug'] )
22+
23+
params = getCreds()
24+
params['debug'] = 'yes'
25+
response = debugAccessToken( params )
26+
27+
print "\nData Access Expires at: "
28+
print datetime.datetime.fromtimestamp( response['json_data']['data']['data_access_expires_at'] )
29+
30+
print "\nToken Expires at: "
31+
print datetime.datetime.fromtimestamp( response['json_data']['data']['expires_at'] )
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import requests
2+
import json
3+
4+
def getCreds() :
5+
creds = dict()
6+
creds['access_token'] = 'ACCESS-TOKEN'
7+
creds['client_id'] = 'FB-APP-CLIENT-ID'
8+
creds['client_secret'] = 'FB-APP-CLIENT-SECRET'
9+
creds['graph_domain'] = 'https://graph.facebook.com/'
10+
creds['graph_version'] = 'v6.0'
11+
creds['endpoint_base'] = creds['graph_domain'] + creds['graph_version'] + '/'
12+
creds['debug'] = 'no'
13+
14+
return creds
15+
16+
def makeApiCall( url, endpointParams, debug = 'no' ) :
17+
data = requests.get( url, endpointParams )
18+
19+
response = dict()
20+
response['url'] = url
21+
response['endpoint_params'] = endpointParams
22+
response['endpoint_params_pretty'] = json.dumps( endpointParams, indent = 4 )
23+
response['json_data'] = json.loads( data.content )
24+
response['json_data_pretty'] = json.dumps( response['json_data'], indent = 4 )
25+
26+
if ( 'yes' == debug ) :
27+
displayApiCallData( response )
28+
29+
return response
30+
31+
def displayApiCallData( response ) :
32+
print "\nURL: "
33+
print response['url']
34+
print "\nEndpoint Params: "
35+
print response['endpoint_params_pretty']
36+
print "\nResponse: "
37+
print response['json_data_pretty']
1.75 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from defines import getCreds, makeApiCall
2+
3+
def getLongLivedAccessToken( params ) :
4+
""" Get long lived access token
5+
6+
API Endpoint:
7+
https://graph.facebook.com/{graph-api-version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={your-access-token}
8+
9+
Returns:
10+
object: data from the endpoint
11+
12+
"""
13+
14+
endpointParams = dict()
15+
endpointParams['grant_type'] = 'fb_exchange_token'
16+
endpointParams['client_id'] = params['client_id']
17+
endpointParams['client_secret'] = params['client_secret']
18+
endpointParams['fb_exchange_token'] = params['access_token']
19+
20+
url = params['endpoint_base'] + 'oauth/access_token'
21+
22+
return makeApiCall( url, endpointParams, params['debug'] )
23+
24+
params = getCreds()
25+
params['debug'] = 'yes'
26+
response = getLongLivedAccessToken( params )
27+
28+
print "\n ---- ACCESS TOKEN INFO ----\n"
29+
print "Access Token:"
30+
print response['json_data']['access_token']

0 commit comments

Comments
 (0)