I want to use Instagram's Graph API on my backend server to retrieve data about an Instagram post. On my frontend, users will submit a post URL (like https://www.instagram.com/p/CAvPIm2lszQ/). Then on my backend, I want to take the ID of the post from that URL (so in this case CAvPIm2lszQ
) and then I'm hoping that I can pass that ID thru the Instagram Graph API and then retrieve the data that I need (media URL, caption, poster username, etc.).
So would that be possible? I did find documentation on "IG Media" for the Graph API, but under permissions, it says, "A Facebook User access token from a User who created the IG Media object, with the following permissions.."
Unless I'm misunderstanding it, I'm not sure if I'll be able to access posts from various public accounts. I think it's also worth mentioning that my users are not logging into their Instagram accounts to use my service so the only possible "User access token" would be my own.
Any ideas on how I can go about this? I was using the instagram.com/p/{post_id}/?__a=1
endpoint to meet my needs before but it doesn't work on my production server for some reason. So I'm kind of stuck.
Most probably you will not be able to achieve that using Instagram API. First of all the ID you are referring to CAvPIm2lszQ
is not the ID that you will use for getting IG Media. The ID is different (it's numeric value like in the sample request from the page you've linked). The full URL that includes CAvPIm2lszQ
is in the shortcode
field.
At the moment it is not possible to look for the post detail using shortcode. If you want to use that endpoint you need to get the real post ID first, for instance by listing list of posts from given user.
But in order to do so - you need to use Facebook login authorization window to get token from given user. Alternatively you can try to request https://developers.facebook.com/docs/instagram-api/guides/business-discovery but it requires going through App review and having your own company to pass the Business Verification. Keep in mind that this endpoint returns information only about Instagram Professional accounts (Business/Creator account). You will not be able to get information about regular accounts.
And the last thing about ?__a=1
endpoint. This is not the official Instagram API. They use it only for their own purposes. Most probably your server IP address has been blocked due to sending too many requests.
This python function below gets the shortcode of a post (the code from the post's url) and retrives some data about it.
def fetch_detailed_post_info(shortcode):
query_hash = "2b0673e0dc4580674a88d426fe00ea90"
variables = {
"shortcode": shortcode
}
variables_json = json.dumps(variables, separators=(',', ':'))
url = f"https://www.instagram.com/graphql/query/?query_hash={query_hash}&variables={variables_json}"
response = requests.get(url)
data = None
if response.status_code == 200:
data = response.json()
# two examples, there is a lot more data fields in the response:
user_data = data['data']['shortcode_media']['owner']
video_media_url = data['data']['shortcode_media'].get('video_url', None)
# etc...
return data
Keep in mind that instagram can rate limit you or block your IP, so if you want to do it in large scale consider residential proxies or some other tricks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With