Skip to content

Commit 670e0d2

Browse files
committed
instagram graph api get user posts and account info with python
1 parent 2e04b68 commit 670e0d2

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from defines import getCreds, makeApiCall
2+
3+
def getAccountInfo( params ) :
4+
""" Get info on a users account
5+
6+
API Endpoint:
7+
https://graph.facebook.com/{graph-api-version}/{ig-user-id}?fields=business_discovery.username({ig-username}){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}&access_token={access-token}
8+
9+
Returns:
10+
object: data from the endpoint
11+
12+
"""
13+
14+
endpointParams = dict()
15+
endpointParams['fields'] = 'business_discovery.username(' + params['ig_username'] + '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}'
16+
endpointParams['access_token'] = params['access_token']
17+
18+
url = params['endpoint_base'] + params['instagram_account_id']
19+
20+
return makeApiCall( url, endpointParams, params['debug'] )
21+
22+
params = getCreds()
23+
params['debug'] = 'no'
24+
response = getAccountInfo( params )
25+
26+
print "\n---- ACCOUNT INFO -----\n"
27+
print "username:"
28+
print response['json_data']['business_discovery']['username']
29+
print "\nwebsite:"
30+
print response['json_data']['business_discovery']['website']
31+
print "\nnumber of posts:"
32+
print response['json_data']['business_discovery']['media_count']
33+
print "\nfollowers:"
34+
print response['json_data']['business_discovery']['followers_count']
35+
print "\nfollowing:"
36+
print response['json_data']['business_discovery']['follows_count']
37+
print "\nprofile picture url:"
38+
print response['json_data']['business_discovery']['profile_picture_url']
39+
print "\nbiography:"
40+
print response['json_data']['business_discovery']['biography']

instagram_graph_api/python/defines.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def getCreds() :
1212
creds['debug'] = 'no'
1313
creds['page_id'] = 'FB-PAGE-ID'
1414
creds['instagram_account_id'] = 'INSTAGRAM-BUSINESS-ACCOUNT-ID'
15+
creds['ig_username'] = 'IG-USERNAME'
1516

1617
return creds
1718

-1.85 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from defines import getCreds, makeApiCall
2+
3+
def getUserMedia( params, pagingUrl = '' ) :
4+
""" Get users media
5+
6+
API Endpoint:
7+
https://graph.facebook.com/{graph-api-version}/{ig-user-id}/media?fields={fields}&access_token={access-token}
8+
9+
Returns:
10+
object: data from the endpoint
11+
12+
"""
13+
14+
endpointParams = dict()
15+
endpointParams['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username'
16+
endpointParams['access_token'] = params['access_token']
17+
18+
if ( '' == pagingUrl ) :
19+
url = params['endpoint_base'] + params['instagram_account_id'] + '/media'
20+
else :
21+
url = pagingUrl
22+
23+
return makeApiCall( url, endpointParams, params['debug'] )
24+
25+
params = getCreds()
26+
params['debug'] = 'no'
27+
response = getUserMedia( params )
28+
29+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 1 <<<<<<<<<<<<<<<<<<<<\n"
30+
31+
for post in response['json_data']['data'] :
32+
print "\n\n---------- POST ----------\n"
33+
print "Link to post:"
34+
print post['permalink']
35+
print "\nPost caption:"
36+
print post['caption']
37+
print "\nMedia type:"
38+
print post['media_type']
39+
print "\nPosted at:"
40+
print post['timestamp']
41+
42+
params['debug'] = 'no'
43+
response = getUserMedia( params, response['json_data']['paging']['next'] )
44+
45+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 2 <<<<<<<<<<<<<<<<<<<<\n"
46+
47+
for post in response['json_data']['data'] :
48+
print "\n\n---------- POST ----------\n"
49+
print "Link to post:"
50+
print post['permalink']
51+
print "\nPost caption:"
52+
print post['caption']
53+
print "\nMedia type:"
54+
print post['media_type']
55+
print "\nPosted at:"
56+
print post['timestamp']

0 commit comments

Comments
 (0)