Twitter API - Reasons for “invalid or expired token”

橙三吉。 提交于 2019-11-27 01:31:05

问题


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?


回答1:


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




回答2:


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.




回答3:


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?




回答4:


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.




回答5:


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!




回答6:


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"



回答7:


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




回答8:


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') 
   ));


}



回答9:


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?



来源:https://stackoverflow.com/questions/17636701/twitter-api-reasons-for-invalid-or-expired-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!