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

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/

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}

Sample of Instagram webhook notification data

I am currently doing an integration to the Instagram API and would like my app to receive webhook notifications whenever there is new media on any Instagram account which has authorized my app via OAuth.
I've been able to write the code which sets up my app to receive notifications using the instructions here. However, I noticed that the Instagram API docs contain no information on what the data in a change notification will look like. My searches online have also come back void. I need sample Instagram webhook notification data so I can write code to parse the data and apply it for my purposes.
I would have gone ahead to expose my localhost to the internet via ngrok, so Instagram would send data to my callback URL when I post a photo to my Instagram account. However, I discovered that the Instagram API does not allow you use an ngrok URL as a callback URL.
Now the only option I have is to deploy my partially completed code to an actual web server. Hoping someone else who has previously integrated to the API can share the format of the webhook notification data. I really don't want to have to do deploy my application at this point.
I eventually had to deploy my partially completed code to a web server. I put a logger behind my webhook callback URL to record the notification data sent to it by Instagram. Below is what it looks like:
[
{
"object": "user",
"object_id": "123456789",
"changed_aspect": "media",
"time": 1506338651,
"data": {
"media_id": "1611605324545559190_123456789"
},
"subscription_id": 0
}
]
The Instagram webhook notification payload/data is a JSON array of objects, each of which contains information on a single change to an Instagram account. Each JSON object contains the following fields:
object: A string whose value is always "user"
object_id: The ID of the Instagram account on which the subject change occurred
changed_aspect: A string whose value is always "media"
time: The Unix epoch indicating when the subject change occurred
subscription_id: The ID of the Instagram subscription object through which this notification was sent. A subscription object is created to ask Instagram to send webhook notifications to your app and is essentially a callback URL.
data: A JSON object containing the following fields:
media_id: The ID of the new Instagram media object (photo, video or carousel) whose creation triggered this webhook notification

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)

Instagram API doesn't response correct?

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

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.