Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid or expired token. Request new token via Tweepy?

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

Do tokens have to be hard coded? I have the OAuth and access tokens hard coded right now, but the token seems to expire every 24 hours. What can I do to renew the token programatically? Can I get a new token via Tweepy?

The goal of my script is to run overnight every day without my permission. I have it set as a cronjob. I don't want to do any manual entry.

I am just using the API on 1 account, my account. So I have the username/password, etc.

Normally, I get the token from clicking "Create my access token," then when I check back a few days later, the token is gone and the button to create it re-appears again.

enter image description here

After regenerating and using it for another day, it happened again! Screenshot: http://i.imgur.com/hhDqLnU.png

like image 988
User Avatar asked May 10 '16 20:05

User


1 Answers

Not sure what the goal of your app is, but to answer some of your questions-concerns:

Do tokens have to be hard coded?

No, Depending on nature of the app you are coding, you can request this data upon executing your program via webform, or textfield in a GUI or include it in another file and encrypt it or import the file it is in and use it...

I have the OAuth and access tokens hard coded right now, but the token seems to expire every 24 hours. What can I do to renew the token programatically?

You cant for the API key and Consumer Secret, this has to be done manually. This information should be permanent, based on https://dev.twitter.com/oauth/overview

The access Token is another story, you can use the API to request one apparently: https://dev.twitter.com/oauth/reference/post/oauth/access_token

POST oauth/request_token Allows a Consumer application to obtain an OAuth Request Token to request user authorization. This method fulfills Section 6.1 of the OAuth 1.0 authentication flow. It is strongly recommended you use HTTPS for all OAuth authorization steps. Usage Note: Only ASCII values are accepted for the oauth_nonce

Resource URL https://api.twitter.com/oauth/request_token

Concerning the access token expiration:

How long does an access token last? We do not currently expire access tokens. Your access token will be invalid if a user explicitly rejects your application from their settings or if a Twitter admin suspends your application. If your application is suspended there will be a note on your application page saying that it has been suspended. as per twitter:

What do I do if an access token I have becomes invalid? You should plan that a user’s access token may become invalid at any time and you will need to re-authorize for that user in the case that it does. Ensuring you handle this situation gracefully is imperative for a quality user experience.

According to twitters documentation https://dev.twitter.com/oauth/application-only

API request contains invalid bearer token Using an incorrect or revoked bearer token to make API requests will result in:

HTTP/1.1 401 Unauthorized Content-Type: application/json; charset=utf-8 Content-Length: 61 ...

{"errors":[{"message":"Invalid or expired token","code":89}]}

Maybe this needs resolving on twitters side? I would suggest contacting them directyl https://dev.twitter.com/solutions/customer-service

like image 180
glls Avatar answered Sep 23 '22 06:09

glls