Twitter上传问题使用Twython - 语法错误 - 无效令牌

时间:2018-03-09 22:44:42

标签: python-3.x syntax-error raspberry-pi3 twython

运行以下.py时,我一直收到无效令牌错误。

我尝试将一个简单的照片展位上传到Twitter。 当你按下按钮时,它会拍照,然后上传。

令牌已被XXXX取代。他们是对的。

我似乎无法纠正语法错误。

有什么想法吗?

*

#!/usr/bin/env python
from twython import Twython
from subprocess import call
import time
import random
import RPi.GPIO as GPIO
# Initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(04, GPIO.IN)   # GPIO4 is pin 7
# Twitter Token
consumer_key = 'xxxxx'
consumer_secret = 'xxxxx'
access_token = 'xxxxx'
access_token_secret = 'xxxxx'
SLEEP_DURATION = 10
messages = []
messages.append("Having a great time with Tactical 74. #tactical74 #tactical74photobooth")
messages.append("The Tactical 74 Photo Booth is on site! #tactical74 #tactical74photobooth")
messages.append("Thanks for visiting the Tactical 74 photo booth. #tactical74 #tactical74photobooth")
messages.append("Another happy customer served. #tactical74 #tactical74photobooth")
# wait for the button
while True:
    # if pressed
    if (GPIO.input(04)):
        try:
            # Take a picture
            call("/opt/vc/bin/raspistill -e jpg --vflip -w 320 -h 320 -q 100 -o /tmp/snapshot.jpg", shell=True)

    # Sign in to Twitter
            twitter = Twython(
                                consumer_key,
                                consumer_secret,
                                access_token,
                                access_token_secret
                                )
            # Post a status update with a picture
            photo = open('/tmp/snapshot.jpg', 'rb')

r = random.randint(0, len(messages)-1)
            message = messages[r]
            twitter.update_status_with_media(status=message, media=photo)
        except:
            print("Unexpected error:")

# Sleep so that multiple pictures aren't taken of the same person
        time.sleep(SLEEP_DURATION)

    else:
        time.sleep(0.25)

*

1 个答案:

答案 0 :(得分:0)

我不知道这是否完全解决了问题,但问题的一部分是id中的04GPIO.setup(04, GPIO.IN)

您不能在Python中的数字之前使用if (GPIO.input(04)):。例如:

0

返回:

a = 04

您所能做的就是将04转换为字符串或从代码中完全删除SyntaxError: invalid token

请参阅SyntaxError invalid token