Instagram API doesn't response correct? - api

If i use this link of the Instagram API i get nothing back:
https://api.instagram.com/v1/locations/1/media/recent?access_token=XXXX
Response:
{"pagination": {}, "meta": {"code": 200}, "data": []}
The documentation says that there are to parameters max_id and min_id, but i'm not sure how to use them.
If i use it like this:
https://api.instagram.com/v1/locations/10/media/recent?access_token=XXXX&min_id=1&max_id=20
I get the same result as above.
Both Location IDs (1 and 10) are correct.

That is cause you are in sandbox mode.
In sandbox mode you will only see your posts(and your approved sandbox users) in API, and total posts in API response is also limited to 20.
If you want to see posts in API response in sandbox mode, then you add a post tagged to a location using Instagram app, and then access that location via API, you will see just your post in API response. Once you get public_content permission and go to live mode, you will see all posts.
Here is more info about sandbox mode: https://www.instagram.com/developer/sandbox/
The behavior of the API when you are in sandbox mode is the same as
when your app is live, but comes with the following restrictions:
Data is restricted to sandbox users and the 20 most recent media from each sandbox user
Reduced API rate limits

Related

How to get the Twitter followers count using Twitter API (in 2022)?

I am surprised to see that there is no way to simply get the number of followers for one Twitter account.
I found many answers (Follower count number in Twitter, How to obtain follower count in Twitter API 1.1?, How to get followers count from twitter, Twitter follower count number) which are way too old as they either use services that do not exist anymore, or make use of the Twitter API v1.1 (instead of the current v2 API).
I found an interesting way (but unsupported) to get the number of followers, using the code of their follow button.
https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=elonmusk
However, I am looking for an official way to retrieve this what I think a simple data from Twitter.
Also note that the official Twitter v2 API does not allow to fetch a count but only to list the followers page per page, which is far from what I try to achieve.
https://api.twitter.com/2/users/44196397/followers
Yes, there is a simple way to do this using the Twitter API v2!
The follower and following counts for a user are part of the public_metrics fields in the User object.
The API endpoint and parameters are in the format
https://api.twitter.com/2/users/[ID]?user.fields=public_metrics,[any other fields]
Here's an example of the output, using the twurl command line tool (which handles the authentication etc, you may need to use a library to help with OAuth):
$ twurl -j "/2/users/786491?user.fields=public_metrics,created_at"
{
"data": {
"id": "786491",
"username": "andypiper",
"created_at": "2007-02-21T15:14:48.000Z",
"name": "andypiper.xyz",
"public_metrics": {
"followers_count": 16570,
"following_count": 3247,
"tweet_count": 134651,
"listed_count": 826
}
}
}
In case you want to get followers count not by user id but using user name
https://api.twitter.com/2/users/by/username/{username}?user.fields=public_metrics
Also you will need to create app in Twitter Developer Portal, receive auth token and use it in your request's authorization header
Authorization: Bearer {token}

Instagram API - Getting invalid media id with video shortcode

I'm using the /media/shortcode/shortcode endpoint of instagram to get information about a certain media with a shortcode.
https://www.instagram.com/developer/endpoints/media/#get_media_by_shortcode
The problem is when I pass a photo's shortcode to the endpoint everything works fine and I get the result I'm expecting from Instagram, however, when I pass a video's shortcode to that endpoint, the api returns the following:
{
"meta": {
"code": 400,
"error_type": "APINotFoundError",
"error_message": "invalid media id"
}
}
Which does not make a lot of sense to me since the given endpoint should work just fine with videos.
I was running into this issue too---the Instagram documentation is not super clear, but this is because of the limitations of Sandbox mode.
To help you develop and test your app, the users and media available in Sandbox mode are real Instagram data (i.e. what is normally visible in the Instagram app), but with the following conditions:
Apps in sandbox are restricted to 10 users
Data is restricted to the 10 users and the 20 most recent media from each of those users
Reduced API rate limits
From https://www.instagram.com/developer/sandbox/.
TLDR you'll need to get your app approved to have your other API calls work.
The best and the easiest way to get the media info, either being photo or video is by calling this url (GET method) Insta Fetch Media URL
Sample code written in Python.
import requests, json
def get_media_id(image_url):
url = "https://api.instagram.com/oembed/"
querystring = {"callback": "", "url": image_url}
headers = {
'cache-control': "no-cache"
}
response = requests.request("GET", url, headers=headers, params=querystring)
return json.loads(response.text)

Get historical data using Instagram API endpoints.

I am trying to fetch data using the Instagram API endpoints. Steps that I have followed
1. Register the client app.
2. Got the access token.
3. I am using this access token through out the url endpoints.
Ex. to get the data for recent media I am using this url endpoint: https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN
However as I am in Sandbox mode, as per the documentation I get only the recent 20 media. Whereas my account contains in total 291 media.
Problems that I am facing:
Not able to get next_url in the paginationpart.
Usage of min_id and max_id in the url does not respond to any changes in the output or the data i.e fetched (Still get only 20 records/media)
Ex: https://api.instagram.com/v1/users/self/media/recent?access_token=XXXXXX&min_id=xxxxxx_xxxxx&max_id=xxxxxxx_xxxxxxx
Can anybody provide a solution as to how exactly I should get all of my historical data ?
You're in the sandbox mode so there's no way to achieve what you're trying to do before your app has been reviewed and approved (and gone live). From Instagram API documentation:
After your app has been reviewed and approved, you are ready to make it available to the general public. To switch your client from sandbox to live mode, you can use the button on the top section of the configuration screen for your app. When you are live, any Instagram user will be able to authorize your app, but you will have access only to the permissions that you were granted during the review.
Well, maybe you should try this library that allows you to scrape public info withou auth (client_id or access_token): https://github.com/raiym/instagram-php-scraper
$medias = Instagram::getMedias('kevin', 150);

Instagram api not working for some api calls like follows, followedby etc

I am using instagram api calls for retrieving various details like media, followers list, followed-by list etc... Even though the user has followers api returns, some times with
{"pagination": {}, "meta": {"code": 200}, "data": []}
and sometimes with
{"meta": {"error_type": "APINotAllowedError", "code": 400, "error_message": "you cannot view this resource"}}
https://api.instagram.com/v1/users/1334757492/?access_token=2301663158.7114bad.addf9ad5791945e3a89a7fb1c3fca31c
The above api works fine. Also the api for getting media.
https://api.instagram.com/v1/users/1334757492/followed-by?access_token=2301663158.7114bad.addf9ad5791945e3a89a7fb1c3fca31c
The above api not working. Also api for follows list and some other api's
you must authorize your access-token on every API scope. and run it on sandbox mode. if you want activate it (on your case, you must at least authorize public_content and follower_list scope), visit this link :
activate public_content :
https://api.instagram.com/oauth/authorize/?client_id=[YOUR-CLIENT-ID]&redirect_uri=[YOURREDIRECT-URI]&response_type=token&scope=public_content
activate follower list :
https://api.instagram.com/oauth/authorize/?client_id=[YOUR-CLIENT-ID]&redirect_uri=[YOUR-REDIRECT-URI]&response_type=token&scope=follower_list
reference :
1. https://www.instagram.com/developer/authorization/
2. https://www.instagram.com/developer/sandbox/

Instagram API for ADS posts not working

I get instagram post details (likes, comments) by using the media endpoint:
$data1 = file_get_contents("https://api.instagram.com/v1/media/$media_id/likes?access_token=$access_token");
This works for normal posts from my feed, but when I input the $media_id for an instagram post that is an AD I always get invalid media id
{
"meta": {
"error_type": "APINotFoundError",
"code": 400,
"error_message": "invalid media id"
}
}
Are ads treated as different types of posts or the API is unable to retrieve any details from them?
Yes, the ads are treated like a different type of post and can only be managed or fully read by means of the Facebook APIs. Try the Graph API, the Marketing API or the Business Manager API, that's where I got the most useful tips and results.
(sorry, dont have enough rep to post more than two links. But don't worry, you'll find it on google in a heartbeat)
Instagram ADS API has been integrated into Facebook Marketing API.
All the details are here: https://developers.facebook.com/docs/marketing-api/guides/instagramads/ads_management/v2.6
There is a workaround to get some details on an AD. If you query the oembed endpoint, like this: https://api.instagram.com/oembed/?url=https://www.instagram.com/p/BAZV-jVlFGp/ you get some data.
I wasn't able to get any comments though.