我正试图让我的树莓派使用某个标签在某个用户的 Twitter 上观看推文。当它看到一条符合标准的推文时,它应该拍一张照片,然后将这张照片发回给用户。它一直有效,直到它应该上传图片,然后我得到一个“Twitter API 返回 401(未经授权)”。我不知道为什么它会返回 401,因为凭据更早地适用于脚本的流式传输部分。任何帮助将非常感激!
from twython import Twython
from twython import TwythonStreamer
import sys
import os
APP_KEY = 'X'
APP_SECRET = 'X'
OAUTH_TOKEN = 'X'
OAUTH_TOKEN_SECRET = 'X'
def takePicture(id_str, screen_name):
os.system('raspistill -o ' + str(id_str) + '_@' + str(screen_name) + '.png -t 3000 -w 1080 -h 720 -n -vf')
def photo(id_str, screen_name):
open('/home/pi/' + str(id_str) + '_@' + str(screen_name) + '.png', 'rb')
def uppic(screen_name):
twitter.update_status_with_media(status='@' + str(screen_name) + ' is super awesome!', media=photo)
class MyStreamer(TwythonStreamer):
def on_success(self, data):
print data['user']['screen_name'].encode('utf-8')
print data['id_str'].encode('utf-8')
print data['text'].encode('utf-8')
takePicture(data['id_str'], data['user']['screen_name'])
photo(data['id_str'], data['user']['screen_name'])
uppic(data['user']['screen_name'])
def on_error(elf, status_code, data):
print status_code
stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track='#hashtag @user')