2

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)

*

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
Jay
  • 23
  • 2

1 Answers1

0

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

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 1
    That solved my issue... thank you. I've been staring at that for the whole day! It's true, some times you can't see the forest for the trees. Thanks again. – Jay Mar 10 '18 at 13:24
  • @Jay Great. It happens to all of us don't worry about it, just move on and keep improving. :D – Xantium Mar 10 '18 at 13:30