【问题标题】:Twitter API with urllib2 in python在 python 中使用 urllib2 的 Twitter API
【发布时间】:2014-06-22 23:35:18
【问题描述】:

我想使用 Python 中的 Twitter API 使用查找方法从名称中查找用户 ID。我只是使用

完成了类似的请求
response = urllib2.urlopen('http://search.twitter.com...') 

但是对于这个我需要身份验证。我不认为我可以通过 Google python twitter API 做到这一点,因为它没有查找方法。任何想法如何使用 urllib2 进行身份验证??

【问题讨论】:

    标签: python twitter


    【解决方案1】:

    使用 Twitter API 的实际 Python 库之一可能会更好:

    http://dev.twitter.com/pages/libraries#python

    【讨论】:

      【解决方案2】:

      使用urllib2.Request定义完整的HTTP头:

      request = urllib2.Request( 'http://twitter.com/...' )
      request.add_header( 'Authorization', 'Basic ' + base64.b64encode( username + ':' + password ) )
      response = urllib2.urlopen( request )
      

      但请注意,Twitter 上的基本授权很快就会被禁用,您需要迁移到 OAuth。 Twitter API Wiki 有一些例子。

      【讨论】:

        【解决方案3】:

        【讨论】:

          【解决方案4】:

          使用 Twython 库,这是按名称查找用户 ID 的代码:

          from twython import Twython
          twitter = Twython(APP_KEY, APP_SECRET,OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
          print twitter.show_user(screen_name=USER_NAME)["id"]
          

          我希望这会有所帮助。

          【讨论】:

            【解决方案5】:

            可以使用各种 Twitter API 包装库来完成身份验证。 Twitter Developer Page 中列出了 wrapper API 的完整列表。

            无论库是什么,您都必须实现 OAuth 身份验证。这是使用Python Twitter Tool 的示例代码。

            from twitter import *
            
            MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials')
            if not os.path.exists(MY_TWITTER_CREDS):
                oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET,
                            MY_TWITTER_CREDS)
            
            oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
            
            twitter = Twitter(auth=OAuth(
                oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
            
            # Now search with Twitter
            rel = t.search.tweets(q='whatever')['statuses']
            # Now do whatever you want with rel object
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-03-08
              • 1970-01-01
              • 2014-04-23
              • 1970-01-01
              • 2022-06-29
              • 2015-12-31
              • 2013-01-07
              相关资源
              最近更新 更多