Twitter API / Twython - 显示用户获取用户个人资料图片

时间:2013-06-03 09:23:00

标签: python twitter twython

如何使用Twython获取用户个人资料图片?

我看到show_user()方法,但用api密钥和secret + oauth标记和秘密实例化Twython,并调用此方法返回404: TwythonError: Twitter API returned a 404 (Not Found), Sorry, that page does not exist

从Twython实例化w.o api / oauth键调用相同的方法返回400:TwythonAuthError: Twitter API returned a 400 (Bad Request), Bad Authentication data

我还尝试GET来自https://api.twitter.com/1.1/users/show.json?screen_name=USERSCREENNAME的用户信息,并获得了400。

我很感激twitter api 1.1的身份验证请求的工作示例。在twitter api参考文献中找不到它。

5 个答案:

答案 0 :(得分:5)

您需要使用screen_name参数

调用show_user方法
t = Twython(app_key=settings.TWITTER_CONSUMER_KEY,
            app_secret=settings.TWITTER_CONSUMER_SECRET,
            oauth_token=oauth_token,
            oauth_token_secret=oauth_token_secret)

print t.show_user(screen_name=account_name)

答案 1 :(得分:1)

所有Twitter API v1.1端点都需要身份验证。

这个例子是正确的:Twitter API/Twython - show user to get user profile image

答案 2 :(得分:0)

我按照以下方式解决了我的问题:

    api = 'https://api.twitter.com/1.1/users/show.json'
    args = {'screen_name': account_name}
    t = Twython(app_key=settings.TWITTER_CONSUMER_KEY,
                 app_secret=settings.TWITTER_CONSUMER_SECRET,
                 oauth_token=token.token,
                 oauth_token_secret=token.secret)
    resp = t.request(api, params=args)

这将返回json响应,请参阅twitter docs。所以在我的情况下:resp['profile_image_url_https']为twitter提供正常大小的用户个人资料图片的网址,即48px×48px。

答案 3 :(得分:0)

以下是我如何使用twython获取用户详细信息(Python 3)。您可以在此处引用Json的所有密钥ID:https://dev.twitter.com/docs/api/1.1/get/users/show

from twython import Twython

APP_KEY = 'xxxx'
APP_SECRET = 'xxxxx'
OAUTH_TOKEN = 'xxxx'
OAUTH_TOKEN_SECRET = 'xxx'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

details = twitter.show_user(screen_name='lyanaz')
print (details['profile_image_url']) #Prints profile image URL

答案 4 :(得分:-1)

来自此处的示例:https://github.com/ryanmcgrath/twython/tree/master/examples

from twython import Twython

# Requires Authentication as of Twitter API v1.1
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

avatar = open('myImage.png', 'rb')
twitter.update_profile_image(image=avatar)

这实际上改变了头像,但它应该让你开始。

此处还有如何正确验证:https://github.com/ryanmcgrath/twython#authorization-url