Get Twitter Account followers using API and Google Apps Script
Asked Answered
P

1

0

I am trying to get the list of Twitter followers of a specific Twitter user using Google Apps Script. I found many sources:

Source 1
Source 2
Source 3

but unable to create a link with my requirement. I have following code snippet:

function getFollowers(){

  var twitterKeys = {
    TWITTER_CONSUMER_KEY: '###',
    TWITTER_CONSUMER_SECRET: '###',
    TWITTER_ACCESS_TOKEN: '###',
    TWITTER_ACCESS_SECRET: '###',
  };

  var username = 'Example name'
  var props = PropertiesService.getUserProperties();
  props.setProperties(twitterKeys);
  var url = `https://api.twitter.com/2/users/${username}/followers`

  var options = {
    method: 'GET',
    headers: {
      api:twitterKeys
    },
    muteHttpExceptions: true
  };
  
  var response = UrlFetchApp.fetch(url, options);
  var json = JSON.parse(response.getContentText());
  
  console.log(json)   
}

On runs, it shows following output:

{ "title": "Unauthorized", "type": "about:blank", "status": 401, "detail": "Unauthorized" }

Any advice would be much appreciated to resolve it and get Twitter followers of a specific Twitter user.

Pinkham answered 20/7, 2023 at 4:13 Comment(2)
Are you using the free tier Twitter API?Arsphenamine
thank you for your reply, yes I am using free versionPinkham
A
1

The Twitter API relatively recently upgraded from version 1.1 to version 2, so a lot of the examples you'll find online are probably outdated.

Based on the URL, looks like you are using API v2, and it looks like the only thing you can do on the free version of API v2 is create/delete tweets and get information about your own account. You can see the endpoints that you have access to across tiers here (expand the cards to see the full endpoint).

If you do opt into the paid tiers (Basic is probably sufficient if all you need is individual user metrics), and access an endpoint that you are authorized to, the following code should work for you:

function getFollowers(){
  var username = 'Example name'
  var url = `https://api.twitter.com/2/users/by/${username}`

  var options = {
    method: 'GET',
    headers: {
      'Authorization': `Bearer: ${BEARER_TOKEN}` // Get this token from the twitter dev portal
    },
    muteHttpExceptions: true
  };
  
  var response = UrlFetchApp.fetch(url, options);
  var json = JSON.parse(response.getContentText());
  var userData = json.data;
  
  console.log(userData)
}

(I haven't tried this code, just cobbled it together from the docs here and here.

Adrenal answered 20/7, 2023 at 23:2 Comment(8)
thank you for your answer. Can you please suggest a solution if I am able to get a paid version, and which version should I get?Pinkham
I updated my answer with some code that would theoretically help you if you opt into a paid tier. If this works for you, please mark my answer as accepted to help others find it if they run into similar issues.Adrenal
Thank you for your answer, can you please guide me which version should be enough, Basic? Pro? And your endpoint gives information about user not user's followers.Pinkham
That depends entirely on your needs and your financial standing. The Basic plan is $100/month and the Pro is $5000/month and they each come with their own rate limits. You can review the offerings here: developer.twitter.com/en/portal/productsAdrenal
your endpoint gives information about user not user's followers. So endpoint may be different, right?Pinkham
Yeah it depends on what you want. If you just want the count of followers, the above endpoint is sufficient, you can see the object fields here, but if you want the list of followers, you may need to hit another endpoint, you can review all that on their documentationAdrenal
I apologize one last question: Can we get the information about user profile whether user has dm open or not in its profile?Pinkham
I doubt that that would be available, but you can check in the DM section of their docs to confirm.Adrenal

© 2022 - 2024 — McMap. All rights reserved.