Permalink
Browse files

Dynamic callback URL

  • Loading branch information...
1 parent 293e8e7 commit 56a8724c631637bf81362855cbd728d537c300e6 @michaelhelmick committed Mar 21, 2012
Showing with 50 additions and 4 deletions.
  1. +15 −0 README.md
  2. +1 −1 setup.py
  3. +34 −3 tumblpy.py
View
@@ -11,9 +11,24 @@ pip install python-tumblpy
*Get an authorization url for your user*
```python
+# Without a dynamic callback url
+# This will use the callback url specified in your app
+
+# Go to http://www.tumblr.com/oauth/apps and click your
+# app to find out your dynamic callback url
t = Tumblpy(app_key = '*your app key*',
app_secret = '*your app secret*')
+```
+
+If you wish to have a dynamic callback url, specify ```callback_url``` when you initiate the class.
+
+```python
+t = Tumblpy(app_key = '*your app key*',
+ app_secret = '*your app secret*'
+ callback_url = 'http://example.com/callback/')
+```
+```python
auth_props = t.get_authentication_tokens()
auth_url = auth_props['auth_url']
View
@@ -4,7 +4,7 @@
setup(
name='python-tumblpy',
- version='0.2.0',
+ version='0.3.0',
install_requires=['httplib2', 'oauth2', 'simplejson'],
author='Mike Helmick',
author_email='mikehelmick@me.com',
View
@@ -3,10 +3,11 @@
""" Tumblpy """
__author__ = 'Mike Helmick <mikehelmick@me.com>'
-__version__ = '0.2.0'
+__version__ = '0.3.0'
import urllib
import time
+import inspect
try:
from urlparse import parse_qsl
@@ -27,6 +28,21 @@
except ImportError:
raise ImportError('A json library is required to use this python library. Lol, yay for being verbose. ;)')
+# The following is grabbed from Twython
+# Try and gauge the old OAuth2 library spec. Versions 1.5 and greater no longer have the callback
+# url as part of the request object; older versions we need to patch for Python 2.5... ugh. ;P
+OAUTH_CALLBACK_IN_URL = False
+OAUTH_LIB_SUPPORTS_CALLBACK = False
+if not hasattr(oauth, '_version') or float(oauth._version.manual_verstr) <= 1.4:
+ OAUTH_CLIENT_INSPECTION = inspect.getargspec(oauth.Client.request)
+ try:
+ OAUTH_LIB_SUPPORTS_CALLBACK = 'callback_url' in OAUTH_CLIENT_INSPECTION.args
+ except AttributeError:
+ # Python 2.5 doesn't return named tuples, so don't look for an args section specifically.
+ OAUTH_LIB_SUPPORTS_CALLBACK = 'callback_url' in OAUTH_CLIENT_INSPECTION
+else:
+ OAUTH_CALLBACK_IN_URL = True
+
class TumblpyError(Exception):
""" Generic error class, catch-all for most Tumblpy issues.
@@ -67,7 +83,7 @@ def __str__(self):
class Tumblpy(object):
- def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, headers=None, client_args=None):
+ def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, headers=None, client_args=None, callback_url=None):
# Define some API URLs real quick
self.base_api_url = 'http://api.tumblr.com'
self.api_version = 'v2'
@@ -84,6 +100,8 @@ def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_
self.oauth_token = oauth_token
self.oauth_secret = oauth_token_secret
+ self.callback_url = callback_url
+
self.default_params = {'api_key': self.app_key}
# If there's headers, set them. If not, lets
@@ -120,7 +138,20 @@ def get_authentication_tokens(self):
auth_url = auth_props['auth_url']
print auth_url
"""
- resp, content = self.client.request(self.request_token_url, 'GET')
+ callback_url = self.callback_url or 'oob'
+
+ request_args = {}
+ method = 'GET'
+
+ if OAUTH_LIB_SUPPORTS_CALLBACK:
+ request_args['oauth_callback'] = callback_url
+ else:
+ # Thanks @jbouvier for the following hack
+ # This is a hack for versions of oauth that don't support the callback url
+ request_args['body'] = urllib.urlencode({'oauth_callback': callback_url})
+ method = 'POST'
+
+ resp, content = self.client.request(self.request_token_url, method, **request_args)
status = int(resp['status'])
if status != 200:

0 comments on commit 56a8724

Please sign in to comment.