简体   繁体   English

Twitter流API未经授权

[英]Twitter Streaming API unauthorized

I have a problem getting tweets from twitter streaming api: https://stream.twitter.com/1.1/statuses/filter.json 我从Twitter流API获取推文时遇到问题: https : //stream.twitter.com/1.1/statuses/filter.json

I always get error 401, unauthorized. 我总是收到未经授权的错误401。 Server time is correct. 服务器时间正确。

I already sign request. 我已经签署要求。 Here is my code (python): 这是我的代码(python):

    token = oauth.Token(key = TWITTER_ACCESS_TOKEN, secret = TWITTER_ACCESS_TOKEN_SECRET)
    consumer = oauth.Consumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)

    params = {'oauth_version': '1.0', 'oauth_nonce': oauth.generate_nonce(), 'oauth_timestamp': int(time.time())}
    req = oauth.Request(method='POST', parameters=params, url='https://stream.twitter.com/1.1/statuses/filter.json?track=twitter')
    req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)

    urllib2.urlopen(req.to_url())

What am I doing wrong? 我究竟做错了什么?

Please help me. 请帮我。

Thanks in advance, Sabrina 在此先感谢,Sabrina

You are either providing it with incorrect credentials or there is a bug causing the incorrect signature to be calculated. 您为它提供了不正确的凭据,或者存在一个导致计算不正确签名的错误。 Setting log level to DEBUG might be helpful here. 在这里将日志级别设置为DEBUG可能会有所帮助。

Using my credentials from the twitter application page I had no problems fetching the stream using 使用Twitter应用程序页面中的凭据,我可以轻松地使用来获取流

from requests_oauthlib import OAuth1Session

key = "<your client key>"
secret = "<your client secret>"
token = "<your token here>"
token_secret = "<token secret>"

twitter = OAuth1Session(key, client_secret=secret,
                        resource_owner_key=token,
                        resource_owner_secret=token_secret)

r = twitter.get(
    'https://stream.twitter.com/1.1/statuses/filter.json?track=twitter',
    stream=True
)

for line in r.iter_lines():
    if line:
        print line

which uses requests-oauthlib . 使用requests-oauthlib If the above works for you then your credentials are correct and the issue might be with your oauth library. 如果以上方法适用于您,则您的凭据正确,并且问题可能出在您的oauth库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM