shajivk
shajivk

Reputation: 570

Check User Tokens are valid or not using Twitter Api

I have a Asp.Net application that will post tweets in twitter. I'm using Twitterizer2 for doing this.

First time when the user uses the application, he will be redirected to twitter for authentication. And then the user-token will be stored in my application , so that the user will never be asked again to login to twitter. This is working fine.

Now i want to validate the user-tokens before posting (ie valid token or not) . Is there any way to do this validation?

Upvotes: 1

Views: 3571

Answers (3)

Luís Jesus
Luís Jesus

Reputation: 1678

MoH's code didn't work for me. Here's what I did:

public bool IsTwitterAccessTokenValid(String access_token, String token_secret)
    {
        var token = new Twitterizer.OAuthTokens();
        token.ConsumerKey = this.TwitterConsumerKey;
        token.ConsumerSecret = this.TwitterConsumerSecret;
        token.AccessToken = access_token;
        token.AccessTokenSecret = token_secret;

        var twitterResponse = TwitterAccount.VerifyCredentials(token);

        return (twitterResponse.Result == RequestResult.Success);
    }

Upvotes: 1

shajivk
shajivk

Reputation: 570

I found out the code for validating tokens in another question. The Twitterizer Api itself had the Methods to validate the User tokens. The code is as follows:

Twitterizer.OAuthTokens token = new Twitterizer.OAuthTokens();
token.ConsumerKey = this.AppId;
token.ConsumerSecret = this.AppSecret;
token.AccessToken = userToken;
token.AccessTokenSecret = userSecret;

Twitterizer.TwitterResponse<Twitterizer.TwitterUser> response =
    Twitterizer.TwitterAccount.VerifyCredentials(token);

if (String.IsNullOrEmpty(response.ErrorMessage))
{
    //This is a valid token
}
else
{
    //Invalid token
}

Upvotes: 0

Terence Eden
Terence Eden

Reputation: 14334

You can make a call to the Verify Credentials API

Make an authenticated call to

https://api.twitter.com/1/account/verify_credentials.json 

It will respond with HTTP 200 OK if the tokens are correct - or 401 if they are not.

Upvotes: 2

Related Questions