'Fetch profile photo url from Instagram api
I want to fetch a users instagram profile photo url for an app I'm building. I created a Meta dev account, added my app and then added the Instagram Basic Display product. A dummy account was added to Instagram Testers in order to generate an access token.
So it's possible to get all photo urls by making a request to https://graph.instagram.com/me/media?fields={fields}&access_token={access-token}. But there doesn't seem to be a way of identifying which one is used in the profile.
It appears that there is a profile_pic field, but it's on the Graph api rather than the Basic Display api which I'm using. The Graph api is for businesses or creators so is not appropriate for what I'm building.
Not sure why a publicly available asset like a profile photo is so difficult to obtain via the api.
Note: There may be other questions similar to this out there, but any I've found have been older and on a depreciated version of the api. It's changed a lot recently.
Solution 1:[1]
It turns out that Instagram bizarrely do not provide any standard way of accessing a users profile picture.
The answer Waqas gave was the correct general approach, but requires user to be logged in and is prone to being blocked by Instagram if made with any frequency.
The solution I arrived at involved programatically logging into Instagram using a csrf token that was obtained separately. Then saving the cookie to Firestore for use in future logins so Instagram does not ban the account making the request. Another call to Instagram at the address Waqas gave and using the cookie obtained previously allowed the profile_pic_url_hd value taken from the JSON. Finally, another call to Instagram using the url of the profile pic in order to save the photo itself to Firestore.
Solution 2:[2]
You can get publicly available assets like the profile photo once are logged in. Try www.instagram.com/{username}/channel/?__a=1 and get the data from profile_pic_url or profile_pic_url_hd fields.
"profile_pic_url": "https://instagram.fkul10-1.fna.fbcdn.net/v/t51.2885-19/s150x150/237125289_314538050357301_4146672331065148847_n.jpg?_nc_ht=instagram.fkul10-1.fna.fbcdn.net&_nc_cat=109&_nc_ohc=bFSJzUFbs6EAX9eZnpD&edm=AAWvnRQBAAAA&ccb=7-4&oh=00_AT8FiYOqxVuxvzPvfKClC7cm5IrLszF2ctooM08-_dVUQw&oe=6202628B&_nc_sid=e7738c",
"profile_pic_url_hd": "https://instagram.fkul10-1.fna.fbcdn.net/v/t51.2885-19/s320x320/237125289_314538050357301_4146672331065148847_n.jpg?_nc_ht=instagram.fkul10-1.fna.fbcdn.net&_nc_cat=109&_nc_ohc=bFSJzUFbs6EAX9eZnpD&edm=AAWvnRQBAAAA&ccb=7-4&oh=00_AT9gj3HCB2OY7c6fYyaxik15mwOKu4bMM7siim1coR4Axw&oe=620263B3&_nc_sid=e7738c",
"requested_by_viewer": false,
"should_show_category": false,
"should_show_public_contacts": false,
"username": "waqas_obeidy",
"connected_fb_page": null,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Isoaxe |
| Solution 2 | Waqas Khalid Obeidy |
