Reputation: 31
So I have a consumer key and consumer secret from tumblr, and I have the following code allowing me to do Oauth authentication, but I have no idea how to actually log in to my own tumblr through python and/or pytumblr. I can't post to my tumblr after successfully using Oauth. Am I supposed to login to my tumblr through the API, or just log in regularly though http with python and THEN use the API? The old tumblr API hasn't worked since sept 2012 I believe, so python-tumblr at here doesn't work any more from what I can tell. Instead I'm using pytumblr from here.
Here's my code:
import urlparse
import oauth2
import pytumblr
REQUEST_TOKEN_URL = 'http://www.tumblr.com/oauth/request_token'
AUTHORIZATION_URL = 'http://www.tumblr.com/oauth/authorize'
ACCESS_TOKEN_URL = 'http://www.tumblr.com/oauth/access_token'
CONSUMER_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
def Test():
consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth2.Client(consumer)
resp, content = client.request(REQUEST_TOKEN_URL, "GET")
request_token = dict(urlparse.parse_qsl(content))
oauthToken = request_token['oauth_token']
oauthSecret = request_token['oauth_token_secret']
print "Request Token:\n"
print " - oauth_token = " + oauthToken + "\n"
print " - oauth_token_secret = " + oauthSecret + "\n"
pytumblrClient = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET, oauthToken, oauthSecret)
response = pytumblrClient.create_text("mytumblr.tumblr.com", body="test")
print response
As you can see there is no password to my tumblr being sent anywhere, and none of the documentation tells me where to send my password. I get a not-authorized response after running the code:
{u'meta': {u'status': 401, u'msg': u'Not Authorized'}, u'response': []}
Is there even a way to post to my own tumblr using the tumblr API?
Upvotes: 3
Views: 1263
Reputation: 349
tl;dr This workaround instructs the user to retrieve the oauth tokens generated by interactive_console.py and hardcode the token values instead of retrieving them programmatically.
NOTE: I'm convinced this workaround somehow violates the entire principle behind oauth's but I can't figure out why the original method doesn't work. Would greatly appreciate input from folks more knowledgable than I as to - 1. why this workaround is not ideal 2. how I can fix the original method so I don't have to do it this way
WORKAROUND:
If all goes well, both your oauth tokens are now stored in ~/.tumblr (pytumblr README), and you should arrive at an interactive python console
Python 2.7.11 (default, May 19 2016, 13:11:38)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
Exit out of the python console since you won't need it (type 'exit()' )
Retrieve your oauth tokens from ~/.tumblr
Hard code your oauth tokens into whatever you were using that was consistently returning the 401 'Unauthorized' error. For me, that looked like this: (note the << >>)
import urlparse
import oauth2
import pytumblr
REQUEST_TOKEN_URL = 'http://www.tumblr.com/oauth/request_token'
AUTHORIZATION_URL = 'http://www.tumblr.com/oauth/authorize'
ACCESS_TOKEN_URL = 'http://www.tumblr.com/oauth/access_token'
CONSUMER_KEY = '<<REDACTED>>'
CONSUMER_SECRET = '<<REDACTED>>'
# don't need this b/c we're not retrieving oauth tokens programmatically anymore
'''
consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth2.Client(consumer)
resp, content = client.request(REQUEST_TOKEN_URL, "GET")
request_token = dict(urlparse.parse_qsl(content))
oauthToken = request_token['oauth_token']
oauthSecret = request_token['oauth_token_secret']
'''
# original API call with oauth tokens retrieved programmatically
#pytumblrClient = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET, oauthToken, oauthSecret)
# NEW API call with oauth tokens hard-coded
pytumblrClient = pytumblr.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET,
'<<HARD_CODED_OAUTH_TOKEN>>', '<<HARD_CODED_OAUTH_TOKEN_SECRET>>')
response = pytumblrClient.create_text("selfiesindumbo", body="TESTING VIA API")
print response
Hope this helps someone!
Upvotes: 2