How to get the real HQ image from the Instagram API? - instagram

Firstly, there are already questions to this topic, but none cover up my problem, entirely, because it's either not the data I need or it's not working properly.
There are services like InstaDP that are able to show you the HQ version of any profile picture from Instagram. Now, I wonder how this is possible?
I did some research and were able to find a higher quality URL when accessing https://www.instagram.com/instagramforbusiness/?__a=1 (see profile_pic_url_hd, answered here). However, InstaDP seems to have a backend that returns a different url that redirects to a way higher quality image: https://instadp-cors-222621.appspot.com/get-hd?id=1107766105 (see at hd_profile_pic_url_info, I extracted the ID for the URL from the result of the ?__a=1 link). I tested this with my personal profile and was able to get the image of myself in an outstanding quality of 1024x1024. However, the ?__a=1 link seem only to return a link for my profile picture in 320x320.
Since InstaDP seem to not be the only player who is able to fetch HQ profile pictures I went ahead and compared the backends of those players. It seems that each service seem to have a different URL to the HQ profile picture of the same Instagram account. So my conclusion is that the Instagram API is involved in all that.
So I created a client key at https://www.instagram.com/developer/. I was also able to get my auth token and determine my logged in csrftoken for the X-CSRFToken header. Now my question is how to continue?
I found a few answers to this topic stating I should request https://i.instagram.com/api/v1/users/1107766105/info/, but it always returns the login page as HTML.
I tried a REST client that uses my Chrome cookies and logged into Instagram before, I tried to set my HTTP headers to X-CSRFToken:<mycookietoken> and Content-Type:application/json. (If I don't set the CSRFToken it errors, so I need to add it, but if the header is set I get the HTML again, even when the CSRFToken is correct. I don't get an error when the CSRFToken is wrong.)
I also tried setting the Origin, Referer and Host to trick Instagram in believing the request came from its own window location, without luck. Setting the Host will even cause a 400 bad request. Even adding my access token in the URL had no effect (?access-token=########).
To sum my question up, how do those services obtain the profile pictures in a such a great quality of up to 1024x1024 from the cdninstagram servers?

Although I am late, but this might be of someone's help:
Step 1:
First thing you need to get HD Instagram profile picture is their profile ID. This can be found in the source code of user profile link. For example if you view source of the following link https://www.instagram.com/abdulhaq0/ and search for "logging_page_id" you will get "profilePage_1285389476". The numbers following the profilePage are the ID for this account.
Step 2:
Next you need put ID in the following URL https://i.instagram.com/api/v1/users/{ProfileID}/info/ and open it in browser. In our case link would be https://i.instagram.com/api/v1/users/1285389476/info/
Step 3:
Now on the link above search for "hd_profile_pic_url_info". There you can get the URL of HD Instagram profile picture.
Hope this helped.

Steps:
1. Get the instagram post link. Eg : https://www.instagram.com/p/Bo-Jru-g7Wa/
or if you don't have the link, the instagram api provides you with a permalink option in the result array which for the above link is Bo-Jru-g7Wa
Now just follow add media?size=l after the url ie.,
Result: High quality image url:
https://www.instagram.com/p/Bo-Jru-g7Wa/media?size=l
you can see it in action here: https://jsfiddle.net/nmj1z7wo/fiddle URL
This link can be considered as a shorthand code to instagram image URL's which are very much bigger

I believe those sites like https://instadp.site/ that show the hi-res of the user profile image do not use the official Instagram API.
In the past it was possible to hack Instagram's CDN URLs to change parameters and get the high resolution from them, but nowadays the URLs are signed and if you change any parameters the URL will fail.
So, the only solution they may be using is to emulate a client. There is a popular PHP client for this: https://github.com/mgp25/Instagram-API

The instagram API have updated. Now we will get only the 320x320 sized image from
https://www.instagram.com/{username}/?__a=1
Even if you get the user id from this endpoint the "user info" endpoint does not return the hd url in its response and should pass a header too now.
import requests
def get_user_by_user_id(user_id):
if user_id:
base_url = "https://i.instagram.com/api/v1/users/{}/info/"
headers = {
'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)'
}
try:
res = requests.get(base_url.format(user_id),headers=headers)
print(res.json())
except Exception as e:
print("getting user failed, due to '{}'".format(e.message))
get_user_by_user_id(userid)
You can check this code and try. Websites like instadp I guess they does not use instagram's official API

The easiest and most reliable way of getting HD (1080x1080) profile pic is Instaloader CLI.
instaloader USERNAME_OF_INTEREST --no-posts --login YOUR_USERNAME

Related

Instagram access token with invalid error

How can I display Instagram images of my account to my website homepage using access token and user id?
I followed this guide and get Instagram access token and user id https://developers.facebook.com/docs/instagram-basic-display-api/getting-started
With this guide I get my long-lived access token
but when I run below code in PHP
$access_token="--elided--";
$photo_count=6;
$json_link="https://api.instagram.com/v1/users/self/media/recent/?";
$json_link.="access_token={$access_token}&count={$photo_count}";
echo json_link;
It shows:
{"meta": {"code": 400, "error_type": "OAuthAccessTokenException", "error_message": "The access_token provided is invalid."}}
How can I show Instagram media on my website?
That guide didn't work for me either. Eventually, I figured this out following the procedure described here.
A few key points to keep in mind:
Tokens are being generated from Facebook for Developers
, with no need for a post request like before.
To fetch the photos, you need to make more than one call to the API: One for fetching the photos' IDs and then, one separate call for each ID to get the actual data (permalink, image URL, caption, etc).
There is a limit to the calls that you can make per hour, which, as I realized, can be easily reached on a site with moderate traffic. So, you need to somehow cache the results (on WordPress, I used a transient for that, which seems to work fine).
The token expires after 60 days and you need to refresh it on time for your app to keep working.
Im kind of late but perhabs try to use different API URL. I was also stuck on this problem, trying to use
https://api.instagram.com/v1/users/self/media/recent/?...
But later found this address
https://graph.instagram.com/me/media?fields=media_url,media_type&access_token=...
It works for me with this URL. Other stuff to put into fields=
I would reccomend testing with either curl in cmd or downloading Postman
Also if you followed that guide, you'll end up with short-lived token. There is a way to exchange it for long-lived token in this tutorial
But all you really need to do is get the URL ready
https://graph.instagram.com/access_token?grant_type=ig_exchange_token&
client_secret={client_secret}&
access_token={token}
where
client_secret is your instagram secret (developers.facebook.com => my apps => => on the left panel click "Instagram basic display")
token is that short-lived token you get from the tutorial mentioned in question (this one)

Instagram server side authentication 2019

I just can't get it right.
There are docs for instagram api that says do one call, it will redirect user to a page where he authenticate, then you get code from Redirect Url and now using this code you can get token. That is working and it is just fine. But, what if i want to do a website for myself as personal page and show my own instagram photos filtered by tag to ANYONE. I don't need OAuth, any login. I'm ok with creating Node server and make a call smth like this:
http://api.instagram.com/auth?client_id_or_login=vvinog&password=secretpassword
and get token from this call. But it looks like in the internet no one do this, or i dump?
Can you please advice some tutorial, approach that allows me to do that?
Any code snippets are super welcome.

Instagram API throwing OAuthAccessTokenException 400 error using client id

I was using the following api to get the latest 3 posts from public accounts to show on the website:
https://api.instagram.com/v1/users/{user-id}/media/recent/?client_id={client-id}&count=3
I had created an app to get the client-id.
However from today, this API has started throwing the following exception:
{
meta: {
error_type: "OAuthAccessTokenException",
code: 400,
error_message: "The access_token provided is invalid."
}
}
Could you please let me know as how to resolve this?
Based on the date, you probably have an older app that got hit by the API migration today, like mine. In short, Instagram decided to make developing for their platform WAY more annoying by requiring all API requests to be authenticated per user, even for data that users shares publicly. So you (like me) will likely be redesigning you app entirely.
To tell, log in to instagram.com/developer and click manage clients; then hit edit next to the set of keys your're trying to use. Up near the top, it will have a section called 'Client Status' -- if yours reads 'Sandbox Mode', fun times ahead! Hopefully you interact with less than 10 users and can stay in sandbox mode, otherwise you'll have to write an essay, film a video, and basically plead to get your permissions back (probably in a few months, when some Instagram intern finally digs his way down to you in the pile of applications). If it reads something eles, you've got another problem altogether and should thank your lucky stars.
In the meantime, I guess I'll get back to sending out dozens of emails to the maintainers of our many, many affiliated Instagram accounts to explain the issue and try to get permissions, so provided we get approved by then, all our social media displays aren't broken during a huge event Saturday. Another option might be to use the OAuth-less json response available here, but that might break terms of service.
I have a solution to this. If you are using the same code I am, which appears likely. I was pulling the last two images using this.
https://api.instagram.com/v1/users/{user-id}/media/recent/?client_id={client-id}&count=3
What I did to get this working is the following.
Login to your Instragram account you are using as the application.
Go to the developer (API) area. https://www.instagram.com/developer/clients/manage/
Manage clients. Make sure your website URL is the same as your valid redirect URL.
Add new Sandbox User. Put in the account of the IG photos you want to reach.
Hit this URL: https://api.instagram.com/oauth/authorize/?client_id=CLIENTID&redirect_uri=REDIRECT_URI&response_type=token where the client ID is the same one you used in your previous app above.
You should get back and access token URL. Copy your access token.
Login as your account that you want the IG photos of. The account you added as a sandbox user and go to developer and approve the Sandbox Invites.
Change your original URL above from https://api.instagram.com/v1/users/{user-id}/media/recent/?client_id={client-id}&count=3 to https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS_TOKEN with your access token.
This is the IG API Media endpoint documentation: https://www.instagram.com/developer/endpoints/users/
After that, it all worked for me and while you are in the sandbox, you should be able to pull the last 3 photos or at this point, figure out how to read the JSON to do so.
Has your app been approved after the June 1st Instagram platform changes?
http://developers.instagram.com/post/145262544121/instagram-platform-update-effective-june-1-2016
If you want to retrieve the user media file then try this, It's working for me
https://graph.instagram.com/me/media?fields=id,caption,media_url,media_type&access_token=ACCESS_TOKEN
For some reason the token is no more valid. Request it again.
Possible reasons why a token is no more valid:
changed password
verified the account
logged-in from a different country

Instagram Media Search Comes Back with No Results

When I run the following url to access Instagram media search:
https://api.instagram.com/v1/media/search?lat=37.775&lng=-122.4183333&access_token=MyAccessToken
I get the response below from Instagram:
{"meta":{"code":200},"data":[]}
Don't know why I am not getting the detailed media search results back. This url is similar to the example provided in the Instagram/Developer webpage: https://www.instagram.com/developer/endpoints/media/
First question would be, are you running your app in Sandbox mode or Live mode?
If you are running in Sandbox mode and you registered your app after November 17th, then you most likely only have permission to fetch data from users that accepted your invitation to Sandbox. Therefore, you wont be able to get public content even if you specified public_content scope.
Take a look at official documentation page in the top corner you will find the announcement .

Browser Said Invalid Request When I Tried to Access a Picture of Instagram

Recently, I wrote an app to synchronize photos in my Instagram to another platform. The GET /v1/users/self/media/recent?access_token=ACCESS_TOKEN result should contains the links to my pictures, like
https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s640x640/e35/sh0.08/11330602_751572388302808_972254350_n.jpg,
and it went very well. But today after clicking the link, it told me Invalid Request. I'm not sure what happened to this cdn link. Any hints would be appreciated.

Resources