Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify OAuth Token on Twitter

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?

like image 385
shanethehat Avatar asked May 27 '11 16:05

shanethehat


People also ask

How do I get my Twitter OAuth token?

Generating access tokensLogin to your Twitter account on developer.twitter.com. Navigate to the Twitter app dashboard and open the Twitter app for which you would like to generate access tokens. Navigate to the "Keys and Tokens" page. Select 'Create' under the "Access token & access token secret" section.

How can I check my OAuth token?

There are two ways to verify a token: locally or remotely with Okta. The token is signed with a JSON Web Key (JWK) using the RS256 algorithm. To validate the signature, Okta provides your application with a public key that can be used.

How do I verify my Twitter credentials?

Visit your account settings page. Select “Require a verification code when I sign in.” Click on the link to “add a phone” and follow the prompts. After you enroll in login verification, you'll be asked to enter a six-digit code that we send to your phone via SMS each time you sign in to twitter.com.


2 Answers

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

like image 70
abraham Avatar answered Oct 21 '22 10:10

abraham


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.

like image 40
pfluggs11 Avatar answered Oct 21 '22 09:10

pfluggs11