19

I'm storing the oauth info from Twitter in a Flash Cookie after the user goes though the oauth process. Twitter says that this token should only expire if Twitter or the user revokes the app's access.

Is there a call I can make to Twitter to verify that my stored token has not been revoked?

shanethehat
  • 15,460
  • 11
  • 57
  • 87

4 Answers4

18

All API methods that require authentication will fail if the access token expires. However the specific method to verify who the user is and that the access token is still valid is GET account/verify_credentials

Alex Rintt
  • 1,618
  • 1
  • 11
  • 18
abraham
  • 46,583
  • 10
  • 100
  • 152
  • 6
    BTW- this is https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials now, for version 1.1 of the API – Eric G Mar 19 '13 at 20:43
4

This question may be old, but this one is for the googlers (like myself).

Here is the call to twitter using Hammock:

    RestClient rc = new RestClient {Method = WebMethod.Get};
            RestRequest rr = new RestRequest();
            rr.Path = "https://api.twitter.com/1/account/verify_credentials.json";
            rc.Credentials = new OAuthCredentials
                                 {
                                     ConsumerKey = /* put your key here */,
                                     ConsumerSecret = /* put your secret here */,
                                     Token = /* user access token */,
                                     TokenSecret = /* user access secret */,
                                     Type = OAuthType.AccessToken
                                 };
            rc.BeginRequest(rr, IsTokenValid);

Here is the response:

    public void IsTokenValid(RestRequest request, RestResponse response, object userState)
    {
        if(response.StatusCode == HttpStatusCode.OK)
        {
            var user = userState;
            Helper.SaveSetting(Constants.TwitterAccess, user);
        }
        else
        {
            Dispatcher.BeginInvoke(() => MessageBox.Show("This application is no longer authenticated "))
        }
    }

I always borrow solutions from SO, this is my first attempt at giving back, albeit quite late to the question.

pfluggs11
  • 166
  • 2
  • 9
1

I am using TwitterOAuth API and here is the code based on the accepted answer.

$connection     =   new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $twitter_oauth_token, $twitter_oauth_secret); 
$content        =   $connection->get("account/verify_credentials");                        
if($connection->getLastHttpCode() == 200):
    // Connection works fine.
else:
    // Not working
endif;
Web_Developer
  • 1,251
  • 2
  • 18
  • 34
1

When debugging manually:

curl \
   --insecure https://api.twitter.com/1/account/verify_credentials.json?oauth_access_token=YOUR_TOKEN
Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
alex urdea
  • 127
  • 1
  • 2
  • 3
    Version 1 of the twitter api is no longer active. You'll need to use the 1.1 api which requires an oauth flow so you can't just pass a token in any more. – chrisst Jun 29 '16 at 22:59
  • 1
    How would we do this now with 1.1? Shuold the access token and access secret be passed in the headers? What are the keys? – Jonny Sep 08 '17 at 02:02