简体   繁体   中英

How to get user_id using access token from twitter?

I need to get user_id based on access_token from Twitter.

Similar to facebook: https://graph.facebook.com/me?access_token={accessToken}

How can I get similar results using Twitter API, Where I will hit Twitter API using access_token ?

Android app developer sending me userID and authToken when they login using twitter login button on their end (android app). So now I want get user profile detail using this authToken. Please explain me the authToken use and how can get profile detail. Also provide example or reference so , I can understand and implement.

It is possible to retrieve the user Id if you have an access token (both the access token key and the access token secret).

The API endpoint to do this is account/verify_credentials , cf. documentation .

This means that technically, you are able to use twitter API on behalf of the users by storing only their access tokens, no need to keep their userId or usernames in your database - although it would be more convenient to keep these available at hand.

Also important: in the result object, you probably want to use id_str rather than id , as recommended , as numerical id may not be parsed correctly.

Proof-of-concept code in node.js, the same can be achieved in php.

setup:

$ cd /tmp/
$ npm install tuiter
$ node

Then in node:

var keys = {
    "consumer_key" : "xxx"
  , "consumer_secret" : "yyy" 
  , "access_token_key" : "zzzzzzzzzz-zzzz"
  , "access_token_secret" : "aaaaaaa"
}

var tu = require('tuiter')(keys)

tu.verifyCredentials(function(err, res) {
    if (err)
        throw err

    console.log('user id is', res.id_str) // prints the user id
})

Edit

OP precised that the tokens he is referring to are auth_token from the "SIgn in with twitter" button.

As per the documentation :

A successful response contains the oauth_token , oauth_token_secret parameters. The token and token secret should be stored and used for future authenticated requests to the Twitter API. To determine the identity of the user, use GET account / verify_credentials .

The answer provided above should still work then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM