Skip to content

Commit 2e71dec

Browse files
committed
instagram graph api with python
1 parent 1fbd697 commit 2e71dec

2 files changed

Lines changed: 217 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests
2+
import json
3+
4+
def getCreds() :
5+
""" Get creds required for use in the applications
6+
7+
Returns:
8+
dictonary: credentials needed globally
9+
10+
"""
11+
12+
creds = dict() # dictionary to hold everything
13+
creds['access_token'] = 'ACCESS-TOKEN' # access token for use with all api calls
14+
creds['graph_domain'] = 'https://graph.facebook.com/' # base domain for api calls
15+
creds['graph_version'] = 'v6.0' # version of the api we are hitting
16+
creds['endpoint_base'] = creds['graph_domain'] + creds['graph_version'] + '/' # base endpoint with domain and version
17+
creds['instagram_account_id'] = 'INSTAGRAM-BUSINESS-ACCOUNT-ID' # users instagram account id
18+
19+
return creds
20+
21+
def makeApiCall( url, endpointParams, type ) :
22+
""" Request data from endpoint with params
23+
24+
Args:
25+
url: string of the url endpoint to make request from
26+
endpointParams: dictionary keyed by the names of the url parameters
27+
28+
29+
Returns:
30+
object: data from the endpoint
31+
32+
"""
33+
34+
if type == 'POST' : # post request
35+
data = requests.post( url, endpointParams )
36+
else : # get request
37+
data = requests.get( url, endpointParams )
38+
39+
response = dict() # hold response info
40+
response['url'] = url # url we are hitting
41+
response['endpoint_params'] = endpointParams #parameters for the endpoint
42+
response['endpoint_params_pretty'] = json.dumps( endpointParams, indent = 4 ) # pretty print for cli
43+
response['json_data'] = json.loads( data.content ) # response data from the api
44+
response['json_data_pretty'] = json.dumps( response['json_data'], indent = 4 ) # pretty print for cli
45+
46+
return response # get and return content
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import time
2+
from defines_py3 import getCreds, makeApiCall
3+
4+
def createMediaObject( params ) :
5+
""" Create media object
6+
7+
Args:
8+
params: dictionary of params
9+
10+
API Endpoint:
11+
https://graph.facebook.com/v5.0/{ig-user-id}/media?image_url={image-url}&caption={caption}&access_token={access-token}
12+
https://graph.facebook.com/v5.0/{ig-user-id}/media?video_url={video-url}&caption={caption}&access_token={access-token}
13+
14+
Returns:
15+
object: data from the endpoint
16+
17+
"""
18+
19+
url = params['endpoint_base'] + params['instagram_account_id'] + '/media' # endpoint url
20+
21+
endpointParams = dict() # parameter to send to the endpoint
22+
endpointParams['caption'] = params['caption'] # caption for the post
23+
endpointParams['access_token'] = params['access_token'] # access token
24+
25+
if 'IMAGE' == params['media_type'] : # posting image
26+
endpointParams['image_url'] = params['media_url'] # url to the asset
27+
else : # posting video
28+
endpointParams['media_type'] = params['media_type'] # specify media type
29+
endpointParams['video_url'] = params['media_url'] # url to the asset
30+
31+
return makeApiCall( url, endpointParams, 'POST' ) # make the api call
32+
33+
def getMediaObjectStatus( mediaObjectId, params ) :
34+
""" Check the status of a media object
35+
36+
Args:
37+
mediaObjectId: id of the media object
38+
params: dictionary of params
39+
40+
API Endpoint:
41+
https://graph.facebook.com/v5.0/{ig-container-id}?fields=status_code
42+
43+
Returns:
44+
object: data from the endpoint
45+
46+
"""
47+
48+
url = params['endpoint_base'] + '/' + mediaObjectId # endpoint url
49+
50+
endpointParams = dict() # parameter to send to the endpoint
51+
endpointParams['fields'] = 'status_code' # fields to get back
52+
endpointParams['access_token'] = params['access_token'] # access token
53+
54+
return makeApiCall( url, endpointParams, 'GET' ) # make the api call
55+
56+
def publishMedia( mediaObjectId, params ) :
57+
""" Publish content
58+
59+
Args:
60+
mediaObjectId: id of the media object
61+
params: dictionary of params
62+
63+
API Endpoint:
64+
https://graph.facebook.com/v5.0/{ig-user-id}/media_publish?creation_id={creation-id}&access_token={access-token}
65+
66+
Returns:
67+
object: data from the endpoint
68+
69+
"""
70+
71+
url = params['endpoint_base'] + params['instagram_account_id'] + '/media_publish' # endpoint url
72+
73+
endpointParams = dict() # parameter to send to the endpoint
74+
endpointParams['creation_id'] = mediaObjectId # fields to get back
75+
endpointParams['access_token'] = params['access_token'] # access token
76+
77+
return makeApiCall( url, endpointParams, 'POST' ) # make the api call
78+
79+
def getContentPublishingLimit( params ) :
80+
""" Get the api limit for the user
81+
82+
Args:
83+
params: dictionary of params
84+
85+
API Endpoint:
86+
https://graph.facebook.com/v5.0/{ig-user-id}/content_publishing_limit?fields=config,quota_usage
87+
88+
Returns:
89+
object: data from the endpoint
90+
91+
"""
92+
93+
url = params['endpoint_base'] + params['instagram_account_id'] + '/content_publishing_limit' # endpoint url
94+
95+
endpointParams = dict() # parameter to send to the endpoint
96+
endpointParams['fields'] = 'config,quota_usage' # fields to get back
97+
endpointParams['access_token'] = params['access_token'] # access token
98+
99+
return makeApiCall( url, endpointParams, 'GET' ) # make the api call
100+
101+
params = getCreds() # get creds from defines
102+
103+
params['media_type'] = 'IMAGE' # type of asset
104+
params['media_url'] = 'https://justinstolpe.com/sandbox/ig_publish_content_img.png' # url on public server for the post
105+
params['caption'] = 'This image was posted through the Instagram Graph API with a python script I wrote! Go check out the video tutorial on my YouTube channel.'
106+
params['caption'] += "\n."
107+
params['caption'] += "\nyoutube.com/justinstolpe"
108+
params['caption'] += "\n."
109+
params['caption'] += "\n#instagram #graphapi #instagramgraphapi #code #coding #programming #python #api #webdeveloper #codinglife #developer #coder #tech #developerlife #webdev #youtube #instgramgraphapi" # caption for the post
110+
111+
imageMediaObjectResponse = createMediaObject( params ) # create a media object through the api
112+
imageMediaObjectId = imageMediaObjectResponse['json_data']['id'] # id of the media object that was created
113+
imageMediaStatusCode = 'IN_PROGRESS';
114+
115+
print( "\n---- IMAGE MEDIA OBJECT -----\n" ) # title
116+
print( "\tID:" ) # label
117+
print( "\t" + imageMediaObjectId ) # id of the object
118+
119+
while imageMediaStatusCode != 'FINISHED' : # keep checking until the object status is finished
120+
imageMediaObjectStatusResponse = getMediaObjectStatus( imageMediaObjectId, params ) # check the status on the object
121+
imageMediaStatusCode = imageMediaObjectStatusResponse['json_data']['status_code'] # update status code
122+
123+
print( "\n---- IMAGE MEDIA OBJECT STATUS -----\n" ) # display status response
124+
print( "\tStatus Code:" ) # label
125+
print( "\t" + imageMediaStatusCode ) # status code of the object
126+
127+
time.sleep( 5 ) # wait 5 seconds if the media object is still being processed
128+
129+
publishImageResponse = publishMedia( imageMediaObjectId, params ) # publish the post to instagram
130+
131+
print( "\n---- PUBLISHED IMAGE RESPONSE -----\n" ) # title
132+
print( "\tResponse:" ) # label
133+
print( publishImageResponse['json_data_pretty'] ) # json response from ig api
134+
135+
params['media_type'] = 'VIDEO' # type of asset
136+
params['media_url'] = 'https://justinstolpe.com/sandbox/ig_publish_content_vid.mp4' # url on public server for the post
137+
params['caption'] = 'This video was posted through the Instagram Graph API with a python script I wrote! Go check out the video tutorial on my YouTube channel.'
138+
params['caption'] += "\n."
139+
params['caption'] += "\nyoutube.com/justinstolpe"
140+
params['caption'] += "\n."
141+
params['caption'] += "\n#instagram #graphapi #instagramgraphapi #code #coding #programming #python #api #webdeveloper #codinglife #developer #coder #tech #developerlife #webdev #youtube #instgramgraphapi" # caption for the post
142+
143+
videoMediaObjectResponse = createMediaObject( params ) # create a media object through the api
144+
videoMediaObjectId = videoMediaObjectResponse['json_data']['id'] # id of the media object that was created
145+
videoMediaStatusCode = 'IN_PROGRESS';
146+
147+
print( "\n---- VIDEO MEDIA OBJECT -----\n" ) # title
148+
print( "\tID:" ) # label
149+
print( "\t" + videoMediaObjectId ) # id of the object
150+
151+
while videoMediaStatusCode != 'FINISHED' : # keep checking until the object status is finished
152+
videoMediaObjectStatusResponse = getMediaObjectStatus( videoMediaObjectId, params ) # check the status on the object
153+
videoMediaStatusCode = videoMediaObjectStatusResponse['json_data']['status_code'] # update status code
154+
155+
print( "\n---- VIDEO MEDIA OBJECT STATUS -----\n" ) # display status response
156+
print( "\tStatus Code:" ) # label
157+
print( "\t" + videoMediaStatusCode ) # status code of the object
158+
159+
time.sleep( 5 ) # wait 5 seconds if the media object is still being processed
160+
161+
publishVideoResponse = publishMedia( videoMediaObjectId, params ) # publish the post to instagram
162+
163+
print( "\n---- PUBLISHED IMAGE RESPONSE -----\n" ) # title
164+
print( "\tResponse:" ) # label
165+
print( publishVideoResponse['json_data_pretty'] ) # json response from ig api
166+
167+
contentPublishingApiLimit = getContentPublishingLimit( params ) # get the users api limit
168+
169+
print( "\n---- CONTENT PUBLISHING USER API LIMIT -----\n" ) # title
170+
print( "\tResponse:" ) # label
171+
print( contentPublishingApiLimit['json_data_pretty'] ) # json response from ig api

0 commit comments

Comments
 (0)