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

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}

Related

Using Instagram's Graph API, how can I retrieve post data (media url, caption, poster username, etc)

I want to use Instagram's Graph API on my backend server to retrieve data about an Instagram post. On my frontend, users will submit a post URL (like https://www.instagram.com/p/CAvPIm2lszQ/). Then on my backend, I want to take the ID of the post from that URL (so in this case CAvPIm2lszQ) and then I'm hoping that I can pass that ID thru the Instagram Graph API and then retrieve the data that I need (media URL, caption, poster username, etc.).
So would that be possible? I did find documentation on "IG Media" for the Graph API, but under permissions, it says, "A Facebook User access token from a User who created the IG Media object, with the following permissions.."
Unless I'm misunderstanding it, I'm not sure if I'll be able to access posts from various public accounts. I think it's also worth mentioning that my users are not logging into their Instagram accounts to use my service so the only possible "User access token" would be my own.
Any ideas on how I can go about this? I was using the instagram.com/p/{post_id}/?__a=1 endpoint to meet my needs before but it doesn't work on my production server for some reason. So I'm kinda stuck.
Most probably you will not be able to achieve that using Instagram API. First of all the ID you are referring to CAvPIm2lszQ is not the ID that you will use for getting IG Media. The ID is different (it's numeric value like in the sample request from the page you've linked). The full URL that includes CAvPIm2lszQ is in the shortcode field.
At the moment it is not possible to look for the post detail using shortcode. If you want to use that endpoint you need to get the real post ID first, for instance by listing list of posts from given user.
But in order to do so - you need to use Facebook login authorization window to get token from given user. Alternatively you can try to request https://developers.facebook.com/docs/instagram-api/guides/business-discovery but it requires going through App review and having your own company to pass the Business Verification. Keep in mind that this endpoint returns information only about Instagram Professional accounts (Business/Creator account). You will not be able to get information about regular accounts.
And the last thing about ?__a=1 endpoint. This is not the official Instagram API. They use it only for their own purposes. Most probably your server IP address has been blocked due to sending too many requests.

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

Twitter API 1.1 - Retrieving tweets of other users

I am working on a project, that compiles the tweets of 4 or 5 different twitter accounts related to a particular subject - and I do not control them.
This was possible with the 1.0 API. A simple ajax call to the API would return the JSON, and off you went.
With the implementation of OAuth for Version 1.1, it seems as though it is no longer possible to retrieve the tweets from accounts you do not own.
My question, is there a work around using the current API to retrieve tweets from another user? Perhaps using the GET search/tweets interface?
The API call you need is statuses/user_timeline
As the documentation says
Returns a collection of the most recent Tweets posted by the user indicated by the screen_name or user_id parameters.
So, if you want all of my tweets, you would call
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=edent
You will need to be authenticated with the Twitter API. I suggest that you use a library for your preferred language.

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.

Grab username off twitter

How can I grab the number of favorites off of a page like this? https://twitter.com/DaltonMetzler4
Like how can I grab the # of followers and number of favorites?
You'll need to use the Twitter REST API: https://dev.twitter.com/rest/public and understand how to authenticate your application with OAuth.
Followers: https://dev.twitter.com/rest/reference/get/followers/ids
Favorites: https://dev.twitter.com/rest/reference/get/favorites/list
You can explore the entire API with the Twitter Console: https://dev.twitter.com/rest/tools/console
Here's a good intro/tutorial to the Twitter API: http://code.tutsplus.com/tutorials/building-with-the-twitter-api-getting-started--cms-22192