Created
September 21, 2022 19:27
-
-
Save ritik1009/13a2e1de06c6517e8eb7db8e12af3976 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def func_get_page_id(access_token = ''): | |
url = graph_url + 'me/accounts' | |
param = dict() | |
param['access_token'] = access_token | |
response = requests.get(url = url,params=param) | |
print("\n response", response) | |
response = response.json() | |
print("\n response", response) | |
page_id = ['data'][0]['id'] | |
print("\n page_id",page_id) | |
return page_id | |
def func_get_instagram_business_account(page_id = '',access_token = ''): | |
url = graph_url + page_id | |
param = dict() | |
param['fields'] = 'instagram_business_account' | |
param['access_token'] = access_token | |
response = requests.get(url = url,params=param) | |
print("\n response",response) | |
response = response.json() | |
print("\n response", response) | |
try: | |
instagram_account_id = response['instagram_business_account']['id'] | |
except: | |
return {'error':'Instagram account not linked'} | |
return instagram_account_id | |
def get_post_data(media_id='', access_token=''): | |
url = graph_url + media_id | |
param = dict() | |
param['fields'] = 'caption,like_count,media_url,owner,permalink' | |
param['access_token'] = access_token | |
response = requests.get(url=url, params=param) | |
response = response.json() | |
return response | |
def func_get_media_id(instagram_account_id = '',access_token = ''): | |
url = graph_url + instagram_account_id +'/media' | |
param = dict() | |
param['access_token'] = access_token | |
response = requests.get(url =url,params = param) | |
response = response.json() | |
media = [] | |
for i in response['data']: | |
media_data = get_post_data(media_id =i['id'],access_token=access_token) | |
media.append(media_data) | |
return media | |
page_id =func_get_page_id(access_token=long_lived_access_token) | |
insta_id = func_get_instagram_business_account(page_id=page_id,access_token=long_lived_access_token) | |
post_data = func_get_media_id(instagram_account_id= insta_id,access_token=long_lived_access_token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment