36

What are the possible reasons that can cause token to become expired (besides having the user un-authorising the app)?

My problem is that I have an app with several thousands of users, all API communication works perfectly but for some users I am getting the invalid or expired token error, my initial though was that they are users who canceled the authentication to the app but I've contacted some of them and they haven't revoked the access.

Any ideas what other issues can cause that error?

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
Ran
  • 3,455
  • 12
  • 47
  • 60
  • 1
    Recently, twitter has changed its api. Now 180 messages can be posted per hour from external api. If user crosses the limit, then twitter will unauthenticate them from sending any further message. Please check your application, if it crosses the limit. – Krishna Rani Sahoo Jul 22 '13 at 15:07
  • 3
    thank you @krishnasahoo I am aware of the changes made in version 1.1 of the API in terms of rate limits. but when crossing the rate limit you get a "rate limit" error and not the error i've described above – Ran Jul 22 '13 at 16:05
  • thank you for the elaborated reply, i'm already familiar with what you mentioned above and i dont think that is the problem since if i wasnt complying to one of those issues, none of the API calls would pass and as i mentioned it only happens to a small portion of my users. currently my guess is that it happens to users who are changing their twitter user name, maybe when doing so the user is automatically de-authenticated from all of the apps. – Ran Aug 01 '13 at 17:21

11 Answers11

20

Check the integrity of an access token at any time by calling the GET account/verify_credentials while using that access token.

Its mentioned and by research I came to know that:

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.

Why is my twitter oauth access token invalid / expired ?

Check this post: invalid / expired access tokens.

There is one post in google groups that says:

You don't get a second chance, and this is by design. OAuth requests have a unique signature; once a particular request is submitted, it can't be submitted again. If they enter the pin correctly, all is well, you get an access token. If they enter the pin wrong, you get 401 Unauthorized - which is expected. But if they then try again to enter the pin, even the correct pin shows as unauthorized.

Check this link for the above reference.

Some suggestions by twitter employee for the same problem:

I guess there are two things I would suggest at this point: 1.) Go to your application settings and use the "Reset keys" tab to reset your consumer key and secret, then update those values in the app and verify that you still see the same behavior. 2.) Try passing oauth_callback in your request_token call. Honestly I don't think this will make a difference, but I want to try and be as rigorous as I can here.

Also check this discussion saying:

You need to use the oauth_token and oauth_token_secret returned from the oauth/access_token call instead of the one in your app's settings in dev.twitter.com

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • thank you for the elaborated reply, i'm already familiar with what you mentioned above and i dont think that is the problem since if i wasnt complying to one of those issues, none of the API calls would pass and as i mentioned it only happens to a small portion of my users. currently my guess is that it happens to users who are changing their twitter user name, maybe when doing so the user is automatically de-authenticated from all of the apps. – Ran Aug 01 '13 at 17:21
  • Please confirm if user changed there account primary information. If there is something that the twitter app can't trust, it will reject the tokens. Please read the twitter API faqs https://dev.twitter.com/docs/faq. For troubleshooting you should read Troubleshooting OAuth 1.0A - Link - https://dev.twitter.com/discussions/204 – sjain Aug 02 '13 at 07:12
  • Well , I used user.accessToken/Secret .. and it was working great , right now it's returning undefined , but the App AccessToken/Secret still working without problem . – Hamza May 18 '15 at 12:41
13

I was getting same error then I changed (access_token) to (access_token_key) and it worked for me.

I hope it will help someone.

ankitkhandelwal185
  • 1,023
  • 1
  • 15
  • 24
6

In addition to the comments everyone else has made, sometimes the twitter api will return a "invalid token" error when the token isn't the problem. I've noticed it the most when I've built a request string that doesn't parse correctly. For example, once I was getting that error when I was passing in screen_name's that had symbols that weren't URI-encodable. I also have gotten it when I passed in empty values like this (where the cursor is empty):

https://api.twitter.com/1/followers.json?cursor=&screen_name=whatevah

Could you give us the specifics of the calls that are returning this error?

milestyle
  • 931
  • 7
  • 14
1

Have you confirmed that the tokens worked at one time? In an OAuth system I worked on, there was an error in how tokens were securely stored and retrieved that caused a small percentage of them to become corrupted. If you can confirm that the tokens worked in the past, that's a good first step.

When you retrieve the tokens from storage, are they unchanged? Is it possible for them to get corrupted with the way you're managing them?

Put some logging in place to keep track of when tokens work and fail. Does a token ever start working again after it has failed once? If you fail to use a token for 30 days, does it expire? With a detailed log, you can start identifying the expired tokens and look for patterns in use to point to what might cause them to expire.

Be sure to explore other possibilities as well. How do users revoke tokens in Twitter? Is it easy to accidentally do that? For users with failed tokens, do they have other authorized apps that have stopped working as well?

tomlogic
  • 11,489
  • 3
  • 33
  • 59
1

First of all nice question Ran.

I want to ask you that have you gone through Twitter developers??

Sometimes it becomes ambiguous that which token to use since Twitter provides two pairs of tokens and the library.One of them is a secret key.

You need to select those token which starts with your Twitter ID followed by a hyphen.

Now your question is this error happens with some of yours users. So here is the answer that an app itself finds ambiguous to choose the token.

Though I might not be completely right, but I recommend you to try this solution at least once.

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
1

It might be possible these users have not revoked access. But in my experience an access token can also get expired after the user (in test cases: me) changed his/her password.

When the user does that, you can no longer use the REST API of stream API on that user's scope. Please adapt your application to handle with that situation. Revoke the user's session, so when he comes back to your application, he/she can be redirected to Twitter again to start a new OAuth access token process. Or send him/her an e-mail to kindly ask to reconnect. Vimeo/Windows/... are some of the people handling expired tokens with e-mails.

Have fun!

hannesvdvreken
  • 4,858
  • 1
  • 15
  • 17
1

My God's answer is correct but I will share my answer from another question explaining how it could be your computer's clock:

If your OAuth flow was working one day and failing the next, check your computer's clock. I was running a Vagrant box that somehow had its time set to the day before, which caused the Twitter API to return {"code":89,"message":"Invalid or expired token."}. This may also appear as 401 timestamp out of bounds. You can use this command to update your clock in Ubuntu:

sudo ntpdate time.nist.gov

Alternative method if ntpdate isn't available on your system:

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
Community
  • 1
  • 1
Zack Morris
  • 4,727
  • 2
  • 55
  • 83
  • After traveling to a different time zone, I ran into this issue with all accounts using Twitter oauth on my dev system. On a mac: simply switching the ntp server in system settings to one of the other options seems to have resolved the issue. – soychicka May 08 '18 at 20:24
1

if your Access Token=738629462149844993-FcWHjfcucCLGEosyGGQ38qI******iC then don't forget to mention hyphen (-) followed by your USERID.

0

May be this will be helpful for you.I faced the same problem.

Please find the below piece of code snippet

$code = $tmhOAuth->user_request(array(
      'method' => 'POST',
     'url' => $tmhOAuth->url('oauth/access_token', ''),
     'params' => array(
            'oauth_verifier' => trim($params['oauth_verifier']),
        )
      ));

   if ($code == 200) {
        $oauth_creds = $tmhOAuth->extract_params($tmhOAuth->response['response']); 
        // echo '<pre>';print_r($oauth_creds);exit;


      $tmhOAuth->reconfigure(array_merge($tmhOAuth->config, array(
         'token'  => $oauth_creds['oauth_token'],
            'secret' => $oauth_creds['oauth_token_secret'],
     )));

  $code = $tmhOAuth->user_request(array(
          'url' => $tmhOAuth->url('1.1/account/verify_credentials') 
   ));


}
Amit
  • 224
  • 1
  • 10
  • 1
    This error occurs when the token you're using is either expired or invalid. Verify that the strings you're using for access token and access token secret are valid. You may have inadvertently expired the tokens and need to regenerate them. – Amit Jul 17 '13 at 04:41
  • in order to do what yo described i need to re-send the user to twitter and re-authenticate, this is something i wish to avoid... – Ran Jul 18 '13 at 04:38
0

The error invalid or expired token can be linked with the fact that one is not paying.

Without paying one will only be able to create the dev environment (sandbox).

As I have answered here:

Counts is only available to paid premium accounts, and one needs to pay for premium access.

Use this link to Apply for access.

Community
  • 1
  • 1
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
0

Try to regenerate the keys again and save them properly.

For me, it happened because after regenerating one of the keys I did not update other keys. Therefore removed and regenerated all 4 keys again (CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET). And tried to execute it again and it worked this time.

Yogesh Awdhut Gadade
  • 2,498
  • 24
  • 19