I am having trouble with a "Bad Authentication" error when reading my API keys from a file as such:
#!/usr/local/bin/python
import tweepy
#open a file called "keys" with keys and tokens for Twitter separated by newlines
keyFile = open('keys', 'r')
consumer_key = keyFile.readline()
consumer_secret = keyFile.readline()
access_token = keyFile.readline()
access_token_secret = keyFile.readline()
keyFile.close()
print "consumer key: " + consumer_key
print "consumer secret: " + consumer_secret
print "access token: " + access_token
print "access token secret: " + access_token_secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
If I manually set my keys, like with consumer_key = "xxx"
, it works fine. Any tips on why is doesn't work when reading from file? Thanks.
So it turns out Python was reading the newline characters as well. The solution was to strip the hidden characters with rstrip():
consumer_key = keyFile.readline().rstrip()