【问题标题】:Twitter API/Twython - show user to get user profile imageTwitter API/Twython - 显示用户以获取用户个人资料图片
【发布时间】:2013-05-29 10:00:00
【问题描述】:

如何使用 Twython 获取用户头像?

我看到show_user() 方法,但是使用 api 密钥和秘密 + 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。

我还尝试从https://api.twitter.com/1.1/users/show.json?screen_name=USERSCREENNAME 获取GET 用户信息,也得到了400。

我希望有一个对 twitter api 1.1 进行身份验证的请求的工作示例。在 twitter api 参考上找不到它。

【问题讨论】:

    标签: python twitter twython


    【解决方案1】:

    这是我如何使用 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
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您需要使用 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)
        

        【讨论】:

        • 是的,你是对的。昨天我以同样的方式使用了另一个端点(get_mentions_timeline)。
        【解决方案4】:

        我通过以下方式解决了我的问题:

            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'] 将 url 提供给 twitter 正常大小的用户个人资料图片,即 48 像素 x 48 像素。

        【讨论】:

          【解决方案5】:

          从这里的例子: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

          【讨论】:

          • 我不想更新个人资料图片,只是为了获取它的链接。我已经有一个适当的身份验证过程。
          • 你说你想要“一个对 twitter api 1.1 进行身份验证的请求的工作示例”。这就是我给你的。
          • 已经过认证,不是如何认证用户,不是oauth dance。带有所有令牌的请求如何?对于已经通过身份验证的用户,如何通过 urllib 或请求 lib 到 twitter api 来查看请求?
          猜你喜欢
          • 1970-01-01
          • 2016-04-28
          • 2022-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多