I'm writing python code to use instagram graph api/business discovery to get data. But I get "Invalid OAuth access token" error but I could not figure out how to solve this problam, please enlighten me.
Following code works fine on Graph Api Explorer
IgUserId?fields=business_discovery.username(bluebottle){followers_count,media_count,name}
But when I operate following code on Python file in Jupyter lab
import requests
import pandas as pd
pd.set_option('display.max_rows', None)
# information
business_account_id = "same as IgUserId"
token = "one that generated on Graph Api Explorer"
fields = "followers_count,media_count,name"
version = "v17.0"
username = "bluebottle"
def user_info(version, igUserId,token,username,fields):
request_url = "https://graph.facebook.com/{version}/{igUserId}?fields={fields}&access_token={access-token}"
response = requests.get(request_url)
return response.json()
print(user_info(business_account_id,token,username,fields,version))
I get following error
{'error': {'message': 'Invalid OAuth access token - Cannot parse access token', 'type': 'OAuthException', 'code': 190, 'fbtrace_id': 'Just for case I'm deleteing this bit'}}
To avoid my misunderstanding to confuse anybody, following is picture of Graph Api Explorer where I got Access Token which I just copy and pasted from top black shadowed bit.
I am not a Python guy, but just to help you I modified your Python script and while doing that, I saw many issues with your script. Use the below script which I tested with my IG User ID and access_token and is working perfectly fine.
import requests
import pandas as pd
pd.set_option('display.max_rows', None)
# information
business_account_id = "YOUR_IG_USER_ID"
access_token = "YOUR_ACCESS_TOKEN"
fields = "followers_count,media_count,name"
version = "v17.0"
username = "bluebottle"
def user_info(version, igUserId,access_token,username,fields):
request_url = f"https://graph.facebook.com/{version}/{igUserId}?fields=business_discovery.username({username}){{{fields}}}&access_token={access_token}"
print(request_url);
response = requests.get(request_url)
return response.json()
print(user_info(version,business_account_id,access_token,username,fields))
Output:-
{'business_discovery': {'followers_count': 442462, 'media_count': 2076, 'name': 'Blue Bottle Coffee', 'id': '17841401441775531'}, 'id': '17841448XXXXX'}
I used Python 3.10.6 to test this script.
Hope this helps.