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'])
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 .
jterrace
source share