Invalid or Expired Token in tweepy app



I’m working on a basic twitter app, using the Tweepy python library, to unfollow people who don’t follow me. Yesterday I had it running and it was working fine. Today it keeps failing: as soon as I run the program, I get an error message: Invalid or Expired Token, code 89. If I go to my Twitter app page in, the app is there and hasn’t been suspended, but on the Keys and Access Tokens tab, it doesn’t have any access token, and says I need to create one. If I create one, and run the program again, the same error happens And If I got back to the app dashboard, same thing, it says I don’t have any access tokens and I need to create some, and so on.
the source code is below. I’ve tried googling and searching here and can’t find a solution. Any help greatly appreciated.

import tweepy, time, sys
ACCESS_KEY = "...-..."
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

followers = api.followers_ids('@...')
friends = api.friends_ids('@...')

for f in friends:
    if f not in followers:
        print "Unfollow {0}?".format(api.get_user(f).screen_name)
        if raw_input("Y/N?") == 'y' or 'Y':


OK I’ve figured it out. It seems to be related to too many requests. I changed this to an automatic unfollow (time.sleep) every 10 seconds and it was still happening. Then I deleted the app, created a new one, and based it on time.sleep every 60 secnods, and it works fine.
The Tweepy doco says it will throw a RateLimitError if you exceed the Twitter API limit, which I assume it does based on what error code response it gets. So either Twitter isn’t throwing the right code (it’s throwing an 89 expired / invalid token instead of something else it should be throwing), or Tweepy isn’t handling it right, i.e. it’s seeing an 89 expired / invalid token and raising a TweepyError instead of RateLimitError).


OK so sorry this is still happening and driving me crazy. I have a python tweepy app that sends out a tweet every half an hour and between then does some friending / unfriending every 10 minutes. Surely this is not exceeding any rate limits or policies. Anyway the program will run for 12 hours, maybe 24 hours, then randomly Twitter will start throwing Invalid or Expired Token. and will never work from that point on.
if I go to and generate a new access token, it wont’ work. I have to delete the app and start again. Then it will work fine for a day or two then stop working.

Can anyone explain this? It is driving me crazy. I’ve tried stack overflow and the forums here and I can’t find a solution. Thanks!


i have the same problem. I expect a rate limit but only get the expired token error 89. Once I go on my app page, my token isn’t even there. I tried changing the timing interval, yet it still won’t work. Making 120 unfollows an hour doesn’t seem like I’m crossing any limits!


That seems like a very automated unfollowing scenario. Automated following and unfollowing is restricted. I am not surprised that an app might have tokens revoked for this kind of behaviour. What is the intent and purpose of your application?