How to handle callback urls

832 views
Skip to first unread message

lankesh87

unread,
Dec 23, 2011, 12:17:50 PM12/23/11
to Django users
Hi,

I am trying to handle a callback url. Following is the code in my
urls.py file:

--------------------------------------------------------------------------------------
urls.py:-
--------------------------------------------------------------------------------------

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('',
# Example:
(r'^tweet/', 'tweety.views.twitter_tweet'),

#in the next line i wish to handle the callback url:-
(r'^return/(?P<qqq>\)', 'tweety.views.twitter_return'),

# Uncomment the admin/doc line below to enable admin
documentation:
#(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
#(r'^admin/', include(admin.site.urls)),
)


--------------------------------------------------------------------------------

In my twitter app i have given callback url as:

"http://127.0.0.1:8001/return/"

but the actual url has some added parameters. Something like this:

"http://127.0.0.1/return/?
oauth_token=SOME_VALUE&oauth_verifier=SOME_VALUE"

SOME_VALUE is alphanumeric.

Now all I need is how to handle this url in Regular Expression

Thanks in advance.

Daniel Roseman

unread,
Dec 23, 2011, 1:31:57 PM12/23/11
to django...@googlegroups.com
As the documentation[1] clearly explains, GET parameters (those after the ?) are not matched in the URLconf. Get them in the view, with request.GET.

---
DR.

lankesh87

unread,
Dec 23, 2011, 1:40:52 PM12/23/11
to Django users
Thanks..
But now I have a new error:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
keyerror description:-
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Environment:

Request Method: GET
Request URL:
http://127.0.0.1:8001/return/?oauth_token=2ec8R2X8d3EfXgcMSR8ZgRVw4HWOH7GbASPykNo&oauth_verifier=FlfW6P2NvSGwZGN6HqM5LwhIekedz1kSIOHNEIJbyzI
Django Version: 1.2.7
Python Version: 2.6.7
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'tweety']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/local/lib/python2.6/site-packages/django/core/handlers/
base.py" in get_response
100. response = callback(request,
*callback_args, **callback_kwargs)
File "/home/lanku/tweet/tweety/views.py" in twitter_return
48. token = oauth2.Token(request.session['request_tok'],
File "/usr/local/lib/python2.6/site-packages/django/contrib/sessions/
backends/base.py" in __getitem__
46. return self._session[key]

Exception Type: KeyError at /return/
Exception Value: 'request_tok'

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I am trying to access the session variable named
session['request_tok']..

but it gives keyerror...


This is the code in my views.py file:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
views.py:-
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def twitter_tweet(request):

_consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth2.Client(_consumer)

resp, content = client.request(REQUEST_TOKEN_URL, 'GET')
if resp['status'] != '200':
raise Exception('Invalid response %s', resp['status'])

request_token1 = dict(urlparse.parse_qsl(content))

request.session['request_tok'] = request_token1['oauth_token']
#dict(cgi.parse_qsl(content))
request.session['request_token_secret'] =
request_token1['oauth_token_secret']
#print "Request Token:"
#print " - oauth_token = %s" %
request_token['oauth_token']
#print " - oauth_token_secret = %s" %
request_token['oauth_token_secret']
#print

# Redirect to Twitter so the user can authorize the application
return HttpResponseRedirect(AUTHORIZATION_URL + '?oauth_token=' +
request_token1['oauth_token'])

# /return Called back by Twitter after the authorization
def twitter_return(request):
token = oauth2.Token(request.session['request_tok'],
request.session['request_token_secret'])
_consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)

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

token = oauth2.Token(access_token['oauth_token'],
access_token['oauth_token_secret'])

client = oauth2.Client(_consumer, token)

resp, content = client.request(TWITTER_POST_STATUS, method='POST',
body=urllib.urlencode( { 'status' : 'This was tweeted from a Python
application' }))

#print resp
#print content

return HttpResponse('Tweeted!')

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> > oauth_token=SOME_VALUE&oauth_verifier=SOME_VALUE<http://127.0.0.1/return/?oauth_token=SOME_VALUE&oauth_verifier=SOME_V...>"
>
> > SOME_VALUE is alphanumeric.
>
> > Now all I need is how to handle this url in Regular Expression
>
> > Thanks in advance.
>
> As the documentation[1] clearly explains, GET parameters (those after the
> ?) are not matched in the URLconf. Get them in the view, with request.GET.
>
> [1]:https://docs.djangoproject.com/en/1.3/topics/http/urls/#what-the-urlc...
> ---
> DR.

Thomas

unread,
Dec 23, 2011, 2:03:44 PM12/23/11
to lankesh87, django...@googlegroups.com

Hi,


Am 23.12.2011 um 19:40 schrieb lankesh87:

> Thanks..
> But now I have a new error:
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> keyerror description:-
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Environment:
>
> Request Method: GET
> Request URL:
> http://127.0.0.1:8001/return/?oauth_token=2ec8R2X8d3EfXgcMSR8ZgRVw4HWOH7GbASPykNo&oauth_verifier=FlfW6P2NvSGwZGN6HqM5LwhIekedz1kSIOHNEIJbyzI

With this URL you have two GET parameters.

You can reach them via request.GET['oauth_token'] and request.GET['oauth_verifier'].


An easy way to find out what comes with the request.GET object is to print it at the begin of your view:

import pprint
pprint.pprint request.GET

good luck,
TR

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

good luck,
TR

------------------------------------------------

http://thoreg.org


Reply all
Reply to author
Forward
0 new messages