Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Dynamic callback URL

  • Loading branch information...
commit 56a8724c631637bf81362855cbd728d537c300e6 1 parent 293e8e7
Mike Helmick authored

Showing 3 changed files with 50 additions and 4 deletions. Show diff stats Hide diff stats

  1. +15 0 README.md
  2. +1 1  setup.py
  3. +34 3 tumblpy.py
15 README.md
Source Rendered
@@ -11,9 +11,24 @@ pip install python-tumblpy
11 11 *Get an authorization url for your user*
12 12
13 13 ```python
  14 +# Without a dynamic callback url
  15 +# This will use the callback url specified in your app
  16 +
  17 +# Go to http://www.tumblr.com/oauth/apps and click your
  18 +# app to find out your dynamic callback url
14 19 t = Tumblpy(app_key = '*your app key*',
15 20 app_secret = '*your app secret*')
  21 +```
  22 +
  23 +If you wish to have a dynamic callback url, specify ```callback_url``` when you initiate the class.
  24 +
  25 +```python
  26 +t = Tumblpy(app_key = '*your app key*',
  27 + app_secret = '*your app secret*'
  28 + callback_url = 'http://example.com/callback/')
  29 +```
16 30
  31 +```python
17 32 auth_props = t.get_authentication_tokens()
18 33 auth_url = auth_props['auth_url']
19 34
2  setup.py
@@ -4,7 +4,7 @@
4 4
5 5 setup(
6 6 name='python-tumblpy',
7   - version='0.2.0',
  7 + version='0.3.0',
8 8 install_requires=['httplib2', 'oauth2', 'simplejson'],
9 9 author='Mike Helmick',
10 10 author_email='mikehelmick@me.com',
37 tumblpy.py
@@ -3,10 +3,11 @@
3 3 """ Tumblpy """
4 4
5 5 __author__ = 'Mike Helmick <mikehelmick@me.com>'
6   -__version__ = '0.2.0'
  6 +__version__ = '0.3.0'
7 7
8 8 import urllib
9 9 import time
  10 +import inspect
10 11
11 12 try:
12 13 from urlparse import parse_qsl
@@ -27,6 +28,21 @@
27 28 except ImportError:
28 29 raise ImportError('A json library is required to use this python library. Lol, yay for being verbose. ;)')
29 30
  31 +# The following is grabbed from Twython
  32 +# Try and gauge the old OAuth2 library spec. Versions 1.5 and greater no longer have the callback
  33 +# url as part of the request object; older versions we need to patch for Python 2.5... ugh. ;P
  34 +OAUTH_CALLBACK_IN_URL = False
  35 +OAUTH_LIB_SUPPORTS_CALLBACK = False
  36 +if not hasattr(oauth, '_version') or float(oauth._version.manual_verstr) <= 1.4:
  37 + OAUTH_CLIENT_INSPECTION = inspect.getargspec(oauth.Client.request)
  38 + try:
  39 + OAUTH_LIB_SUPPORTS_CALLBACK = 'callback_url' in OAUTH_CLIENT_INSPECTION.args
  40 + except AttributeError:
  41 + # Python 2.5 doesn't return named tuples, so don't look for an args section specifically.
  42 + OAUTH_LIB_SUPPORTS_CALLBACK = 'callback_url' in OAUTH_CLIENT_INSPECTION
  43 +else:
  44 + OAUTH_CALLBACK_IN_URL = True
  45 +
30 46
31 47 class TumblpyError(Exception):
32 48 """ Generic error class, catch-all for most Tumblpy issues.
@@ -67,7 +83,7 @@ def __str__(self):
67 83
68 84
69 85 class Tumblpy(object):
70   - def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, headers=None, client_args=None):
  86 + def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, headers=None, client_args=None, callback_url=None):
71 87 # Define some API URLs real quick
72 88 self.base_api_url = 'http://api.tumblr.com'
73 89 self.api_version = 'v2'
@@ -84,6 +100,8 @@ def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_
84 100 self.oauth_token = oauth_token
85 101 self.oauth_secret = oauth_token_secret
86 102
  103 + self.callback_url = callback_url
  104 +
87 105 self.default_params = {'api_key': self.app_key}
88 106
89 107 # If there's headers, set them. If not, lets
@@ -120,7 +138,20 @@ def get_authentication_tokens(self):
120 138 auth_url = auth_props['auth_url']
121 139 print auth_url
122 140 """
123   - resp, content = self.client.request(self.request_token_url, 'GET')
  141 + callback_url = self.callback_url or 'oob'
  142 +
  143 + request_args = {}
  144 + method = 'GET'
  145 +
  146 + if OAUTH_LIB_SUPPORTS_CALLBACK:
  147 + request_args['oauth_callback'] = callback_url
  148 + else:
  149 + # Thanks @jbouvier for the following hack
  150 + # This is a hack for versions of oauth that don't support the callback url
  151 + request_args['body'] = urllib.urlencode({'oauth_callback': callback_url})
  152 + method = 'POST'
  153 +
  154 + resp, content = self.client.request(self.request_token_url, method, **request_args)
124 155
125 156 status = int(resp['status'])
126 157 if status != 200:

0 comments on commit 56a8724

Please sign in to comment.
Something went wrong with that request. Please try again.