1

I am trying to extract followers and following counts of different Twitter users with the help of Tweepy but I keep getting error, [{'code': 89, 'message': 'Invalid or expired token.'}].

All I need is the count (not the list of followers/friends) because some have millions of followers. I could have gone manually to check for each Twitter handle but that is impracticable since I have close to 20000 users I am trying to check out.

Thank you

import tweepy
from requests_oauthlib import OAuth1

# Keys, tokens and secrets
consumer_key = " my api_key"
consumer_secret = "my api_secret_key"
key = "my access_token"
secret = " my access_token_secret"

# Tweepy OAuthHandler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key,secret)
api = tweepy.API(auth)


targets = ['FoxNews', 'XHNews', 'Reuters', 'AP', 'MTV', 'CNN']

for target in targets:
    user = api.get_user(target)
    print(user.name, user.followers_count)

OUTPUT: TweepError: [{'code': 89, 'message': 'Invalid or expired token.'}]

Harmon758
  • 5,084
  • 3
  • 22
  • 39
Swag'O
  • 23
  • 1
  • 4
  • Do you have an actual Twitter API keys for this account and passing it to the API? If so then this link may have something explaining why you'd get this error with a valid key https://stackoverflow.com/questions/17636701/twitter-api-reasons-for-invalid-or-expired-token – Matthew Barlowe Jun 13 '20 at 04:35
  • @MatthewBarlowe thank you! I do have API keys and secret. I followed the link but he discussions to seems to have no definite solutions to the issue. However, I have renewed my API credentials and I am trying it out PyCharm (as against Jupyter NB that I was using before) and it is working fine ...with some errors once in a while though. – Swag'O Jun 14 '20 at 13:05

1 Answers1

0

As the error says, the access token being used is invalid or expired. This is consistent with Twitter's documentation of error code 89 being for Invalid or expired token:

Corresponds with HTTP 403. The access token used in the request is incorrect or has expired.

https://developer.twitter.com/en/support/twitter-api/error-troubleshooting#error-codes

Harmon758
  • 5,084
  • 3
  • 22
  • 39