简体   繁体   中英

Get user id from user name for twitter using python

I am trying to get the user ID for a twitter user using a python

import twitter
twitter_api = twitter.Twitter(domain='api.twitter.com', api_version='1',auth = twitter.oauth.OAuth(ACCESS_KEY, ACCESS_SECRET,
CONSUMER_KEY, CONSUMER_SECRET))
usr = twitter_api.api.GetUser(SCREEN_NAME)
print usr.name

Does not work, and the debug information is not of much help, I did not find any more resources online.

The twitter_api object that you've instantiated implicitly converts all function calls performed on it into the path of the API URL, with any keyword arguments converted to API parameters. For example:

twitter_api.statuses.user_timeline()
# ^- this converts to "api.twitter.com/1/statuses/user_timeline.json"

twitter_api.statuses.user_timeline(user_id="a")
# ^- this converts to "api.twitter.com/1/statuses/user_timeline.json?user_id=a"

twitter_api.statuses.user_timeline(foo)
# ^- this breaks - "foo" is not a key/value pair, and cannot be sent

Hence, you're trying to call the URL 1/api/GetUser.json - and also passing it an argument, which it doesn't know how to handle. Try this instead:

import twitter
t = twitter.Twitter(auth=twitter.OAuth(ACCESS_KEY, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
t.users.lookup(screen_name=SCREEN_NAME)
# returns the user object

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