开发者

Verify OAuth Token on Twitter

开发者 https://www.devze.com 2023-03-09 06:14 出处:网络
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

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开发者_如何学JAVA that my stored token has not been revoked?


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


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.


When debugging manually:

curl \
   --insecure https://api.twitter.com/1/account/verify_credentials.json?oauth_access_token=YOUR_TOKEN


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号