代码之家  ›  专栏  ›  技术社区  ›  Jay

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

  •  2
  • Jay  · 技术社区  · 6 年前

    运行以下操作时,我不断收到无效的令牌错误。py。

    我想做的是把一个简单的照片上传到推特上。 当你按下按钮时,它将拍摄一张照片,然后上传。

    代币已替换为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 回复  |  直到 6 年前
        1
  •  0
  •   Xantium    6 年前

    我不知道这是否能完全解决问题,但您的部分问题在于 04 你加入了 GPIO.setup(04, GPIO.IN) ,则, if (GPIO.input(04)):

    您不能使用 0 在Python中的数字之前。例如:

    a = 04
    

    返回值:

    SyntaxError: invalid token
    

    您所能做的就是将04转换为字符串或删除 0 完全来自您的代码。

    请参见 SyntaxError invalid token