401错误访问Twitter使用tweepy与代理

时间:2017-08-26 07:51:50

标签: python twitter tweepy

我必须使用代理来获取流Twitter,这是我的代码。

import twitter, json, urllib2, chardet, tweepy
from tweepy import Stream, OAuthHandler
from tweepy.streaming import StreamListener
proxy = {'http':'http//:127.0.0.1:53899','https':'https://127.0.0.1:53899'}
proxy_support = urllib2.ProxyHandler(proxy)
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
# setting the OAuth
consumer_key = "OsVDCkR7Y6qoUdpbuglJfuEr0"
consumer_secret = ' tBUEwi3LBpIriTYTCGtqnahdoFSAk6piB48vtDZMNz317xVK6U'
access_token = '3154430460-qwpCD2vHsLt0dOrpTDqZvnFQnY5GUS53iyHJy9p'
access_token_secret = ' dCkLKWoCudTJLIO6ZdsUqQWufyrtZyNCiDotLzncXwjDs'
class listener(StreamListener):
    def on_data(self,data):
        print data
        return True

    def on_error(self,status):
        print status

auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
twitterstream = Stream(auth,listener())
# filter the car
twitterstream.filter(track=["car"])

结果是重复401而没有显示错误。

我已检查了我的时区设置,并将其调整为全局时钟区域,但仍无法正常工作。我还将Twitter帐户的时区更改为UNT。但401仍然存在。

任何想法都会有所帮助!

1 个答案:

答案 0 :(得分:1)

更改行:

consumer_secret = ' tBUEwi3LBpIriTYTCGtqnahdoFSAk6piB48vtDZMNz317xVK6U'
#...
access_token_secret = ' dCkLKWoCudTJLIO6ZdsUqQWufyrtZyNCiDotLzncXwjDs'

为:

consumer_secret = 'tBUEwi3LBpIriTYTCGtqnahdoFSAk6piB48vtDZMNz317xVK6U'
#...
access_token_secret = 'dCkLKWoCudTJLIO6ZdsUqQWufyrtZyNCiDotLzncXwjDs'