1

我编写了一个非常简单的 python 脚本,如果匹配过滤器,它会拉入推文并打开 GPIO。我在家里试过,效果很好,但是,在大学网络上,它似乎无法连接到 twitter。

大学网络的详细信息是 WIRELESS SSID: Uni-WiFi WPA2 Enterprise

它使用 PEAP (MSCHAPv2) 进行连接,这意味着我需要输入我的大学用户名和密码。目前网络已连接,我可以浏览互联网,但是当我启动 python 脚本时出现错误:

urllib2.HTTPError: HTTP Error 401: Unauthorized

这是完整的 python 脚本 - 如果有任何机构可以提供帮助,那就太棒了,这需要尽快提交!

#!/usr/bin/env python

import twitter
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'
from termcolor import colored

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.cleanup()

#My app keys and secrets
CONSUMER_KEY = 'TXXGPRg'
CONSUMER_SECRET = 'jRVxtEgf1CQWuan0N8L4a3s'
OAUTH_TOKEN = '528854Jaudhna2K36g4y79oiwUq'
OAUTH_SECRET = 'ZoQEv1deAQ'

FILTER_TAG = u'art'  # Can also be just text, like u'idol', but expect a lot more results!

# We want a continuous stream of events which match a given tag, so we need to use the streaming API.
twitter_stream = twitter.TwitterStream(auth=twitter.OAuth(OAUTH_TOKEN,OAUTH_SECRET,CONSUMER_KEY,CONSUMER_SECRET))

# Now, we don't want every single tweet from the stream, so we'll filter to include only specific text, or a specific tag.
iterator = twitter_stream.statuses.filter(track = FILTER_TAG)

# Now, iterator is a generator which yields a new tweet whenever it sees one. We need to loop over it forever.
for tweet in iterator:

        print colored(tweet.get(u'user', {}).get(u'name'), 'white', 'on_red'), colored(tweet.get(u'text'), 'cyan')

        if "hate" in tweet.get(u'text', u'fake_text_that_never_matches'):  # Now, you need to light up the light for 5 seconds, then shut it off.
            print colored("Switch turned ON!", "red", 'on_yellow')
            GPIO.setup(7, GPIO.OUT)
            GPIO.output(7,True)## Switch on pin 7
            time.sleep(5)## Wait
            GPIO.output(7,False)## Switch off pin 7

        print "----------------------------------------------------------------------------------------------------------------------------------------------------"
4

1 回答 1

1

Part of the OAuth signature is a timestamp, which is generated when you make the request. If your server's time differs too much from Twitter's server time, the Twitter server will reject your request with a 401. So, check the time being returned by the Twitter server and make sure your local machine that is generating the signature matches the same time.

于 2013-10-01T15:55:34.523 回答