将Python和OAuth2与Twitter流式API结合使用

将Python和OAuth2与Twitter流式API结合使用,python,twitter,twitter-oauth,Python,Twitter,Twitter Oauth,twitterv1api现在已经失效了,所以我一直在尝试使用搜索和流式API来整理标签信息。搜索API是有速率限制的,所以如果一个hashtag上有很多条目,您可能会错过一些条目。流媒体似乎是一种方式 使用OAuth2,以下是我的匿名代码: import oauth2 as oauth import json consumer_key = "<consumer key from twitter developer site>" consumer_secret = "<cons

twitterv1api现在已经失效了,所以我一直在尝试使用搜索和流式API来整理标签信息。搜索API是有速率限制的,所以如果一个hashtag上有很多条目,您可能会错过一些条目。流媒体似乎是一种方式

使用OAuth2,以下是我的匿名代码:

import oauth2 as oauth
import json

consumer_key = "<consumer key from twitter developer site>"
consumer_secret = "<consumer secret>"
oauth_token = "<access token>"
oauth_token_secret = "<access token secret>"
consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
access_token = oauth.Token(key=oauth_token, secret=oauth_token_secret)
client = oauth.Client(consumer, access_token)

terms = json.dumps({'track' : 'twitter'})
stream_endpoint = "https://stream.twitter.com/1.1/statuses/filter.json"
response, data = client.request(stream_endpoint,"POST", body=terms, headers={'Content-Type':'application/json'})

我认为您的错误是因为在服务器上使用JSON数据

terms = json.dumps({'track' : 'twitter'})
您应该像这样编写代码

terms = 'track=twitter'

你成功地完成了吗?我让这段代码运行了30分钟,但没有得到响应。
terms = 'track=twitter'
    USER = request.params.get('username', '00000')
    LIMIT = request.params.get('limit', '50')
    REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
    consumer_key    ='424245wfdsfa4'    
    consumer_secret ='afar234252523adsasd'

    if consumer_key is None or consumer_secret is None:
        print 'you need consumer_key & consumer_secret key'
        sys.exit(1)

    signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
    oauth_consumer             = oauth.Consumer(key=consumer_key, secret=consumer_secret)
    oauth_client               = oauth.Client(oauth_consumer)


    response, content = oauth_client.request(REQUEST_TOKEN_URL, 'POST')

    if response['status'] == '200':
        request_token = dict(parse_qsl(content))
    else:
        print 'Invalid response from Twitter requesting  token.........: %s' % response['status']

    endpoint = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' + USER + '&count=' + LIMIT
    response, content = oauth_client.request(endpoint, 'GET')
    url = response['content-location']
    f = urllib2.urlopen(url)
    response = f.read()
    return simplejson.loads(response)