1

Twitter v1 API 现在已经失效,所以我一直在尝试使用 Search 和 Streaming API 来整理主题标签信息。搜索 API 有速率限制,因此如果主题标签上有很多条目,您可能会错过一些。流媒体似乎是要走的路。

这里使用 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'})

我遇到的问题是这总是返回以下消息:

>>>'No filter parameters found. Expect at least one parameter: follow track locations\r\n'
4

2 回答 2

0

我认为你的错误是因为使用 JSON 数据

terms = json.dumps({'track' : 'twitter'})

你应该像这样写你的代码

terms = 'track=twitter'
于 2013-07-18T02:46:29.670 回答
0
    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)
于 2014-03-21T05:30:41.673 回答