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.
Related
I am trying to a send a basic direct message on Twitter, but it isn't recognizing 'create_direct_message'. This is the code I am using:
Client.create_direct_message(participant_id = '129593148134547046', text = 'Hello')
This is the error message:
AttributeError: 'Client' object has no attribute 'create_direct_message'
This is how the tweepy website says how to do it on their website, so I'm not sure why my computer is not recognizing it. Is there some way to update tweepy? Could I be running an old version? Please help!
https://docs.tweepy.org/en/stable/client.html#manage-direct-messages
The following is steps to send a direct message using Tweepy.
On the Twitter developer portal you will need to upgrade your account to elevated.
Once elevated access is approved and create your app, then got user authentication settings.
Set it to the following:
Read and write and Direct message
Native App
Fill in Callback URL and website URL. (its not used by Tweepy, but must be filled)
Going back to the App view select the "Keys and Tokens" tab.
Store all the information but you need the following.
consumer_key
consumer_secret
access_token
access_token_secret
Once you have that information the following sample code should work (fill in the variables).
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitter = tweepy.API(auth)
recipient_id = '129593148134547046' # As per example above.
text = 'hello'
direct_message = api.send_direct_message(recipient_id, text)
print(direct_message.message_create['message_data']['text'])
If it works the print will be your message.
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 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'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
I've been looking for a way to update my Twitter status from a Python client. As this client only needs to access one Twitter account, it should be possible to do this with a pre-generated oauth_token and secret, according to http://dev.twitter.com/pages/oauth_single_token
However the sample code does not seem to work, I'm getting 'could not authenticate you' or 'incorrect signature'..
As there are a bunch of different python-twitter library out there (and not all of them are up-to-date) I'd really appreciate if anybody could point me a library that's currently working for POST requests, or post some sample code!
Update:
I've tried Pavel's solution, and it works as long as the new message is only one word long, but as soon as it contains spaces, i get this error:
status = api.PostUpdate('hello world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 2459, in PostUpdate
self._CheckForTwitterError(data)
File "C:\Python26\lib\site-packages\python_twitter\twitter.py", line 3394, in _CheckForTwitterErro
r
raise TwitterError(data['error'])
python_twitter.twitter.TwitterError: Incorrect signature
If however the update is just one word, it works:
status = api.PostUpdate('helloworld')
{'status': 'helloworld'}
Any idea why this might be happening?
Thanks a lot in advance,
Hoff
You might be interested in this http://code.google.com/p/python-twitter/
Unfortunately the docs don't exist to be fair and last 'release' was in 2009.
I've used code from the hg:
wget http://python-twitter.googlecode.com/hg/get_access_token.py
wget http://python-twitter.googlecode.com/hg/twitter.py
After (long) app registration process ( http://dev.twitter.com/pages/auth#register ) you should have the Consumer key and secret. They are unique for an app.
Next you need to connect the app with your account, edit the get_access_token.py according to instructions in source (sic!) and run. You should have now the Twitter Access Token key and secret.
>>> import twitter
>>> api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret', access_token_key='access_token',
access_token_secret='access_token_secret')
>>> status = api.PostUpdate('I love python-twitter!')
>>> print status.text
I love python-twitter!
And it works for me http://twitter.com/#!/pawelprazak/status/16504039403425792 (not sure if it's visible to everyone)
That said I must add that I don't like the code, so if I would gonna use it I'd rewrite it.
EDIT: I've made the example more clear.
I've been able to solve this problem using another library - so I'll post my solution here for reference:
import tweepy
# http://dev.twitter.com/apps/myappid
CONSUMER_KEY = 'my consumer key'
CONSUMER_SECRET = 'my consumer secret'
# http://dev.twitter.com/apps/myappid/my_token
ACCESS_TOKEN_KEY= 'my access token key'
ACCESS_TOKEN_SECRET= 'my access token secret'
def tweet(status):
'''
updates the status of my twitter account
requires tweepy (https://github.com/joshthecoder/tweepy)
'''
if len(status) > 140:
raise Exception('status message is too long!')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
result = api.update_status(status)
return result
The most recent location for Python-Twitter documentation is now on GitHub (which the google code page points you at.)
You now no longer need to use the command line tool that comes with python-twitter to get the full set of access tokens and secrets, https://dev.twitter.com will let you request them when you register your app.
Once you have the four different credential values, the first thing you want to do is test them by making an API test:
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
print api.VerifyCredentials()
This will show you if your credentials are working or not. If you get an error the next step is to pass in debugHTTP=True to the Api() call - this will cause all of the HTTP conversation to be printed so you can see the Twitter error message.
Once the credentials are working then you can try to call PostUpdate() or even just GetTimeline()
I Have coded some thing related to this question.
import tweepy
consumer_key = Your_consumer_key
consumer_secret = Your_consumer_secret
access_token = Your_access_token
access_token_secret = Your_access_token_secret_key
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
single_tweet = 'hello world'
api.update_status(single_tweet)
print "successfully Updated"