Video URLs from Instagram API Expire and break after a short period of time - instagram

After a short timeframe, Instagram updates the image URL and the one they give you becomes expired. It isn't a scheduled thing so it isn't something you can refetch on a schedule to fix. The URL you have may break at any time. Has anyone found a solution to easily retrieve an Instagram video URL perhaps similar to the way that the linked solution fetches a current version of static images?
Here is an example of an expired URL that comes from the Instagram API:
https://video-iad3-1.cdninstagram.com/v/t50.33967-16/311796704_656982995824312_2200462786629368413_n.mp4?_nc_cat=100&vs=415717487387323_1550039796&_nc_vs=HBksFQAYJEdPQ2psUks0N2hiR2hWVUNBRjE2Z3ZJX25Za2VicV9FQUFBRhUAAsgBABUAGCRHRGw4alJKMmxkNUFBTU1CQU0wMG1qVU5adW9XYnFfRUFBQUYVAgLIAQAoABgAGwGIB3VzZV9vaWwBMRUAACb8mv6DgfHqQBUCKAJDMywXQBIFHrhR64UYEmRhc2hfYmFzZWxpbmVfMV92MREAdQAA&ccb=1-7&_nc_sid=59939d&efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjcyMC5jbGlwcyJ9&_nc_ohc=nav3pTRz9T0AX-csieO&_nc_ht=video-iad3-1.cdninstagram.com&edm=AL-3X8kEAAAA&oh=00_AT9Wj2ue0CNOvRywTzAh2gvCxJl9Bhr1me4nIveXwZ2KNg&oe=634A481F&_nc_rid=6e1aabaa0c
This problem is specifically for Instagram Videos. The same thing happens to images and we are using the solution from this post: Instagram/feed API media URL shows 'URL signature expired' to fix static images and video poster images. But this doesn't work for video.
We've also tried the suggestions of using parameters ?__a=1 but it comes up as a broken page and does not return json as some have suggested.
Currently we are falling back to the poster image, but having the video would obviously be ideal.

Related

Instagram graph API IG hashtag

I need to get media from connected instagram profile by hashtag. For most part, I was successful, however I stumbled upon a few limitation I cannot find workaround for.
When I fetch media on IG user, I can send username as a field and get it back. However, I cannot get username on IG hashtag, it is not listed as field in documentation, and throws error if I send one.
I don't want to get back albums on IG hashtag, just image and video, but I can't seem to find a way to filter by type
What is interesting, I found an example of site that displays username when filtering by hashtag, but the only way I could think of achiving this is to send request for media details for each media I receive. Needless to say, I want to avoid it.
Has anyone had a similar problem and solved it?

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

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

Expired Media Links

Anybody knows how to fix as issue with expired instagram media links ?
Im parsing media and after a few days/weeks/hours the links are expired.
Maybe there is a way to use this links or fix somehow?
I had the same issue and this is how I solved it, in the instagram media api you can fetch the perma_link like https://www.instagram.com/p/Bt3QrphggBw/. Just add
https://www.instagram.com/p/Bt3QrphggBw/media/?size=m. It will redirect to the proper image url.
All of the media links generated by instagram have timestamp components in them, ie. those links have an expiration time (not sure exactly how much but all of the links expire after some time). The workaround is either to keep refreshing the links by calling instagram api or to store the images somewhere and use that link.

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.

Instagram realtime API does not return content IDs?

Current scenario
I have build an application that harvest Instagram images from a specific hashtag.
I use the pagination to fetch all images, and store the data (not the images) locally in a database. The first invocation of the app harvest all images. Subsequent invocations only harvest those newer than the newest image in the local database. Otherwise I would need to make thousands of request to page through all images on popular tags. And that would need to be done every few minutes, if images where to appear without too much delay in the app.
The problem that arises is that when users put tags on old images, these images are then not fetched by my app - because of the only-fetch-new-images performance construct.
Attempt at solution
I looked at the realtime API, but is seems to me it is constructed in a way that makes it unsuable. This is what it sends in a realtime update for a tag:
{
"subscription_id": "2",
"object": "tag",
"object_id": "nofilter",
"changed_aspect": "media",
"time": 1297286541
}
I would have thought that there would be a list of media IDs, representing the new/changed content, from which I could fetch the actual content - but there is not.
My current solution is to fetch new content every few minutes, and then doing full rescans every hour. This is suboptimal both from a user, and a performance perspective.
Question
Is it really not possible to do in a more elegant way? I appreciate that Instagram does not send the full content in the realtime update, but sending the IDs should not be a problem in terms of payload-size. It seems like the API is pretty useless in this regards - the only use-case I can think of, where it would be of any use, is for a "There is new content on you hashtag watch" nofications.
Best,
Torben
What I do with geography search is that everytime I get a update package I query the recent geography endpoint. This works quite well. Im guessing thats the idea with tag subscription aswell, have you tried querying the recent instagrams tag endpoint?
/tags/[tag-name]/media/recent
Paginate the recent media for a tag with the min_tag_id parameter to sort the result based on when it was tagged and not when the photo was taken. We use this in combination with the realtime api at silarapp.com and it works fine.
From the Instagram Api Documenation (my emphasis):
Get a list of recently tagged media. Note that this media is ordered by when the media was tagged with this tag, rather than the order it was posted. Use the max_tag_id and min_tag_id parameters in the pagination response to paginate through these objects. Can return a mix of image and video types.

Resources