I'm trying to setup a stream using the latest version of Twython with Python 2.7.3. I'm trying to reproduce the example in the streaming docs which depend on the OAuth1 docs. Using the following code yields 401 errors until I kill execution:
from twython import Twython
from twython import TwythonStreamer
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
print['text'].encode('utf-8')
def on_error(self, status_code, data):
print status_code
APP_KEY = 'mupAeFE44nOU5IlCo4AO0g' # Consumer key in twitter app OAuth settings
APP_SECRET = 'NOTMYSECRET0zo1WbMAeSIzZgh1Hsj9CrlShonA' # Consumer secret in OAuth settings
twitter = Twython(APP_KEY,APP_SECRET)
auth = twitter.get_authentication_tokens()
OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track = 'twitter')
The values of 'OAUTH_TOKEN' and 'OAUTH_TOKEN_SECRET' end up set to unicode strings. I've set 'APP_KEY' and 'APP_SECRET' as above or as unicode strings both with the same results.
Following the advice in this reported issue I updated both requests and requests-oauthlib without luck.
I don't believe I'm having firewall issues. At this point, I've tried this code on three different boxes all in different locales all with the same results.
Not sure how to proceed at this point. All help appreciated.
What happens when you run something like the code below?
from twython import Twython
CONSUMER_KEY = "****"
CONSUMER_SECRET = "****"
OAUTH_TOKEN = "****"
OAUTH_TOKEN_SECRET = "******"
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
import json
print(json.dumps(twitter.get_user_timeline(screen_name='jonatascd')[0], indent=2))
I've openend an issue at github with a similar question, almost at the same time [1], because I'm with the same problem here (and I've tried in more than one scenario)
UPDATE:
from the solution that came up in the issue [1] - run:
ntpd -q
that is because the system time was a little off.
Well, for me worked.
[1] https://github.com/ryanmcgrath/twython/issues/237
Related
Edit: Updated code. Just keep getting API errors.
How do I post an image from a URL with a status update to twitter using twython and python 3x? I've read the docs, but it only outlines opening local files in directories, and I've looked at a few threads on SO. Here's what I have so far, I'm getting error codes I don't know how to solve.
What do I change to get this image to post? This gives me twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), Could not authenticate you.
the minute it hits twitter.upload
from twython import Twython, TwythonError
import os
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import urllib
import requests
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter.verify_credentials()
url = "https://farm2.staticflickr.com/1127/4605090363_4a96e64ff1.jpg"
response = requests.get(url)
photo = requests.get(response.url).content
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
This solution: twitter.post('/statuses/update_with_media', params = {'status': 'Testing New Status'}, files = {'media': StringIO(photo)})
Gives me a TypeError: initial_value must be str or None, not bytes
I can not get this to work. I have no idea anymore why. Its a primary account. I'm not using "sign in with twitter", its not a web app. Just a worker bot.
I am baffled, I can not figure out what I am doing wrong. It will post text statuses fine, I can search, get user timelines, send DMS, etc. What am I doing wrong?
This turns out to be a type error with respect to what twython expects when uploading media. Pulling an image down remotely and calling content on it gives us a bytes but twython wants an io object.
That there's a 401 in the stack trace is a red herring, but we can give twython what it wants by adding io and reading the request content into a BytesIO.
from twython import Twython, TwythonError
from io import BytesIO
import os
import urllib
import requests
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter.verify_credentials()
url = "https://farm2.staticflickr.com/1127/4605090363_4a96e64ff1.jpg"
response = requests.get(url)
photo = BytesIO(response.content)
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
I'm trying to requests.get() twitter from their API for a Data Science work. I'm trying to follow documentation but is kind of confusing to me. Here is my code...
import requests
import oauth2 as o
consumer_key = 'sdfgsdfgsdfgsdfgsdfg'
consumer_secrete = 'sadfasdfgasdfasdfasdfasdfasdf'
consumer = o.Consumer(key=consumer_key, secret=consumer_secrete)
access_token = 'asdfasdfsadfasdfasdf'
access_token_secret = 'asdfasdfasdfasdfasdf'
acc_token = o.Token(key=access_token, secret=access_token_secret)
client = o.Client(consumer, acc_token)
base_url = 'https://api.twitter.com/1.1/search/tweets.json?q=%22I%20want%20to%20take%20my%20life%20away%22%20near%3A%22Grove%20South%22%20within%3A1500mi&src=typd'
data = requests.get(base_url, client)
print(data)
I'm getting a 400 Response.
There are so many modules and libraries in Python that is hard to know if it is compatible with Python 3 in the first place, then if it is compatible with the API. It is kind of confusing. Does anybody know if I can do this with requests and oauth2 or I need to change my approach?
I've used Twython in the past with great results.
From their documentation:
from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
I'm trying to write a simple twitter bot using python and tweepy. The code is as follows:
import tweepy
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxx'
ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status('hi')
I get the following error:
TweepError: Twitter error response: status code = 400
I would like some info on how to avoid this error
This may be a bug that emerged in the 3.2.0 release of Tweepy. See this issue opened on GitHub. In that issue, TylerGlaiel notes that api.update_status fails if called thus:
api.update_status('Test')
But works if called like so:
api.update_status(status='Test')
I have also found this works. I imagine a fix will be out before long.
This sentence: api.update_status(status='Test') let you know if your API Key and Token is working well. So, if you see:
TweepError: Twitter error response: status code = 400
Possibly means that you need to regenerate your credentiales for any reason. Regenerate your API Key, API Secret Key and your Tokens and will work.
I am trying to get the list of friends of a user using the Python twitter module. For some reason I keep getting 404 errors. I bet I am missing something very basic. Any ideas?
Thanks!
PS: all OAuth parameters replaced with XXXX.
import twitter
CONSUMER_KEY = 'XXXX'
CONSUMER_SECRET = 'XXXX'
OAUTH_TOKEN = 'XXXX'
OAUTH_TOKEN_SECRET = 'XXXX'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
print twitter_api.GetFriends(user='twitter')
Then I get the following error:
TwitterHTTPError: Twitter sent status 404 for URL: 1.1/GetFriends.json using parameters: (oauth_consumer_key=XXXX&oauth_nonce=XXXX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1388098945&oauth_token=XXXX&oauth_version=1.0&user=twitter&oauth_signature=XXXX)
details: {"errors":[{"message":"Sorry, that page does not exist","code":34}]}
If you're using Python-twitter by DeWitt Clinton, then as PepperoniPizza mentioned, you have an error in your OAuth statement. Your code should be changed as follows:
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
Should become
auth = twitter.Api(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token_key=OAUTH_TOKEN, access_token_secret=OAUTH_TOKEN_SECRET)
See here, under section "Using":
https://code.google.com/p/python-twitter/
However, looking at your code, your work doesn't seem to match how that library opens a connection to twitter. Which library are you using? It almost looks like you're using Mike Verdone's Python Twitter Tools.
I have the following code:
import urlparse
import oauth2 as oauth
PROXY_DOMAIN = "twitter1-ewizardii.apigee.com"
consumer_key = '...'
consumer_secret = '...'
consumer = oauth.Consumer(consumer_key, consumer_secret)
oauth_token = '...'
oauth_token_secret = '...'
token = oauth.Token(oauth_token, oauth_token_secret)
client = oauth.Client(consumer, token)
request_token_url = "https://twitter1-ewizardii.apigee.com/1/account/rate_limit_status.json"
resp, content = client.request(request_token_url, "GET", PROXY_DOMAIN)
print resp
print content
However I continue to get the error "error":"Incorrect signature" this was working earlier, and I tried out solutions people have suggested online, generated new credentials etc, but it doesn't seem to work anymore after working for a week like this.
Thanks,
Although I have switched to tweepy for anyone who finds this question this may be of use to you:
http://dev.twitter.com/pages/libraries
It could have been a glitch on the day I was testing as I didn't go back to trying out the oauth-python module since tweepy has been working for me. But that link list all the possible libraries available and is a valuable resource if such a problem arises again.