【问题标题】:How to use twitter search API asynchronously in python?如何在 python 中异步使用 twitter 搜索 API?
【发布时间】:2013-07-14 04:22:52
【问题描述】:

我读了Introduction to Tornado这本书。它在使用 twitter 搜索 API 的应用程序中引入了 tornado 的异步功能。

code如下:

class IndexHandler(tornado.web.RequestHandler):
     @tornado.web.asynchronous
     @tornado.gen.engine
     def get(self):
        query = self.get_argument('q')
        client = tornado.httpclient.AsyncHTTPClient()
        response = yield tornado.gen.Task(client.fetch,
            "http://search.twitter.com/search.json?" + \
            urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
        ...
        self.finish()

它使用 v1 twitter API 来搜索关键字。但是,新的v1.1 twitter API 禁止使用非 oauth 请求。因此,我必须将 oauth 库与我的使用者密钥和访问密钥一起使用来请求 twitter 搜索 API。

def request_twitter(url, http_method = 'GET', post_body = '', http_headers = ''):
    consumer = oauth.Consumer(key = consumer_key, secret = consumer_secret)
    token = oauth.Token(key = access_token, secret = access_secret)
    client = oauth.Client(consumer, token) 
    request = client.request(url, method = http_method, body = post_body, headers = http_headers)
    return request

但是 oauth 客户端不提供异步请求方式。所以我想知道如何在 python 中使用 oauth 客户端向 twitter API 发出异步请求?谢谢。

【问题讨论】:

    标签: python asynchronous oauth twitter tornado


    【解决方案1】:

    看看随 Tornado 提供的 TwitterMixin 并稍微研究一下 its code

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2020-06-17
      • 2017-08-15
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多