python-3.xsyntax-errorraspberry-pi3twython

Twitter upload issues using Twython - Syntax error - Invalid Token


I keep getting an Invalid Token error when I run the following .py.

What I'm trying to do it a simple photo booth upload to Twitter. When you press the button, it will take a pic, then upload it.

The tokens have been replaced with XXXX. They are correct.

I can't seem to correct the syntax error.

Any thoughts?

*

#!/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)

*


Solution

  • I don't know if this fixes things entirely but part of your problem is with the 04 you have in GPIO.setup(04, GPIO.IN), if (GPIO.input(04)):

    You cannot use 0 before numbers in Python. For example:

    a = 04
    

    returns:

    SyntaxError: invalid token
    

    All you can do is to convert 04 to a string or remove the 0 entirely from your code.

    Please see SyntaxError invalid token