Initializing an Oauth client in python for tumblr API using Python-oauth2

I am new to Oauth. In the past, for Twitter applications written in Python, I used the python-oauth2 library to initialize the client as follows:

consumer = oauth.Consumer(key = CONSUMER_KEY, secret = CONSUMER_SECRET) token = oauth.Token(key = ACCESS_KEY, secret = ACCESS_SECRET) client = oauth.Client(consumer, token) 

It was easy, because Twitter provides the keys and secrets of the USER and ACCESS. But now I need to do the same for tumblr. The problem is that tumblr only provides CONSUMER_KEY, CONSUMER_SECRET and these URLs:

 Request-token URL http://www.tumblr.com/oauth/request_token Authorize URL http://www.tumblr.com/oauth/authorize Access-token URL http://www.tumblr.com/oauth/access_token 

Using this data, how can I initialize the client to access the Tumblr API?

UPD

jterrace suggested code that I tried to use before. The problem with this is oauth_callback. If I don’t specify one, the api returns the error "No oauth_callback specified", but if I provide some kind of URL, for example, "http://example.com/oauthcb/" and follow the link http: // www .tumblr.com / oauth / authorize? oauth_token = 9ygTF ..., then click the "Allow" button, tumblr does not display the PIN page, it immediately redirects to this callback URL, which is useless, as this application is for desktop computers. Why is the PIN code not displayed?

UPD 2

The Tumblr API does not support PIN authorization. Use xAuth instead - https://groups.google.com/group/tumblr-api/browse_thread/thread/857285e6a2b4268/15060607dc306c1d?lnk=gst&q=pin#15060607dc306c1d

+7
source share
6 answers

First import the oauth2 module and configure the service url and consumer information:

 import oauth2 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 = 'your_consumer_key' CONSUMER_SECRET = 'your_consumer_secret' consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET) client = oauth2.Client(consumer) 

Step 1: Get the request token. . This is a temporary token that is used when a user authorizes an access token and signs a request to receive the specified access token.

 resp, content = client.request(REQUEST_TOKEN_URL, "GET") request_token = dict(urlparse.parse_qsl(content)) print "Request Token:" print " - oauth_token = %s" % request_token['oauth_token'] print " - oauth_token_secret = %s" % request_token['oauth_token_secret'] 

Step 2. Redirection to the provider. . Since this is a CLI script, we do not redirect. In a web application, you redirect the user to the URL below.

 print "Go to the following link in your browser:" print "%s?oauth_token=%s" % (AUTHORIZATION_URL, request_token['oauth_token']) # After the user has granted access to you, the consumer, the provider will # redirect you to whatever URL you have told them to redirect to. You can # usually define this in the oauth_callback argument as well. oauth_verifier = raw_input('What is the PIN? ') 

Step 3: After the consumer redirects the user back to the oauth_callback URL, you can request an access token approved by the user. You use request token to sign this request. After that, you will throw away the request token and use the returned access token. You should keep this accessible token somewhere safe, like a database, for future use.

 token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(oauth_verifier) client = oauth2.Client(consumer, token) resp, content = client.request(ACCESS_TOKEN_URL, "POST") access_token = dict(urlparse.parse_qsl(content)) print "Access Token:" print " - oauth_token = %s" % access_token['oauth_token'] print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] print 

Now that you have an access token, you can call protected methods with it.

EDIT: It turns out that tumblr does not support the PIN authorization method. Relevant post here .

+11
source

If you just want to get the access token / secret character, you can simply configure the callback URL as: http: // localhost / blah

  • Launch the CLI application (after changing the callback, privacy, and token)
  • Follow the link in your browser.
  • Allow application
  • View the address bar of the page that you were redirected to in the browser after resolving your application. It should look something like this:

http: // localhost / blah? oauth_token = xxxxxxxxxxxxxxxxxxxxxxxxxx0123456789ABCDEFGHIJKLMN & oauth_verifier = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX23X67XXXXXXXXXXX0123456789abcdefghijklmn

Use the value of the request parameter "oauth_verifier" as your PIN: XXXXXXXXXXXXXXXXXXXXXXXXXXX2323676789abcdefghijklmn

The CLI should print your token token and private key.

NTN! Got this work for tumblr as follows :)

+6
source

Take a look at https://github.com/ToQoz/Pyblr

It uses oauth2 and urllib to provide a good wrapper for exactly what you are trying to do.

0
source

It seems that you are trying to access the OAuth 1 API with the OAuth 2 client.

See https://github.com/simplegeo/python-oauth2 and find the “three-legged OAuth example”.

0
source

There was a problem with oauth2 and facebook. @deepvanbinnen reply lead me in the right direction.

facebook actually redirected to a page similar to this

"HTTP: // local / blah code = AQAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX # _ = _

using the 'AQAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX # _ = _ as the PIN I actually have access to the requested facebook account.

0
source

@jterrance answer is good. However, be aware that this is one _time_ manual procedure for obtaining an access token. An access token is the key that you use for all subsequent API calls. (That's why he recommends storing an access token in the database.) A string called a PIN (also a confirmation key) is not necessarily a number. It can be a printed line in any form. This verification key is displayed on the authorization page at the URL printed in step 2, then inserted into the PIN prompt.

0
source

All Articles