I am trying to get a base example that will post a Tweet on behalf of a user. I have my app setup for read/write:
This application will be able to:
Read Tweets from your timeline.See who you follow, and follow new people.Update your profile.Post Tweets for you.
but it yields {u’errors’: [{u’message’: u’Could not authenticate you.’, u’code’: 32}]}
What am I missing here?
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
import requests
from requests_oauthlib import OAuth1
from urlparse import parse_qs
REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"
AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token="
ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
CONSUMER_KEY="direct from Appliucation settings of https://apps.twitter.com/app/#/keys"
CONSUMER_SECRET="direct from Appliucation settings of https://apps.twitter.com/app/#/keys"
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""
def setup_oauth():
"""Authorize your app via identifier."""
# Request token
oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET)
r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth)
credentials = parse_qs(r.content)
resource_owner_key = credentials.get('oauth_token')[0]
resource_owner_secret = credentials.get('oauth_token_secret')[0]
# Authorize
authorize_url = AUTHORIZE_URL + resource_owner_key
print 'Please go here and authorize: ' + authorize_url
verifier = raw_input('Please input the verifier: ')
oauth = OAuth1(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
# Finally, Obtain the Access Token
r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth)
credentials = parse_qs(r.content)
token = credentials.get('oauth_token')[0]
secret = credentials.get('oauth_token_secret')[0]
return token, secret
def get_oauth():
oauth = OAuth1(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=OAUTH_TOKEN,
resource_owner_secret=OAUTH_TOKEN_SECRET)
return oauth
if __name__ == "__main__":
if not OAUTH_TOKEN:
token, secret = setup_oauth()
print "OAUTH_TOKEN: " + token
print "OAUTH_TOKEN_SECRET: " + secret
oauth= OAuth1(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=token,
resource_owner_secret=secret)
screen_name='username'
status="Hello World @%s!" % screen_name
url='https://api.twitter.com/1.1/statuses/update.json?status='+status
status="Hello World @%s!" % screen_name
r = requests.get(url="https://api.twitter.com/1.1/statuses/mentions_timeline.json", auth=oauth)
r = requests.post(url=url, auth=oauth)#, data=status)
print r.json()
print
else:
oauth = get_oauth()
r = requests.get(url="https://api.twitter.com/1.1/statuses/mentions_timeline.json", auth=oauth)
print r.json()
Note: if I revert to attempt to read the timeline, the return is just an empty JSON even though there is a test tweet on the account.
When prompted for the verifier, I am entering the oauth_verifier value.