[Tutor] Pointers on accessing request object in python webprogramming frameworks

mobiledreamers at gmail.com mobiledreamers at gmail.com
Mon Dec 25 08:39:17 CET 2006


Regarding sessions set by a different application say yahoo mail or MYSPACE
Session set by a different app say yahoo mail api
Hi guys

One app i m developing needs integrating with the client site and get
their users, so I need to use their login to log the users in and once
they are in they should use my application sort of like Microsoft
Passport...

Here is the problem the example usage is in Django

and they are talking about a request object
    if 'session_key' in request.session and 'uid' in request.session:
        fb.session_key = request.session['session_key']

Anyone can tell me how I can access this in webpy
Isnt flup something that is set from webpy, how do we use sessions that
are set by a different Application

This question has a wide range of implications for eg., this can be
used to apply to newly opened yahoo mail api etc., So please share your
insights. I m sharing the example code below..

Thanks



# -----------------------
# Web application example
# -----------------------

def simple_web_app(request, api_key, secret_key):
    fb = WebAppWidget(api_key, secret_key, request.GET['auth_token'])
    fb.auth_getSession()

    friend_ids = fb.friends_get()
    info = fb.users_getInfo(friend_ids, ['name', 'pic'])

    print '<html><body>'
    for friend in info:
        print '<a href="%(pic)s">%(name)s</a>' % friend
    print '</body></html>'

def web_app(request):
    """Get the user's friends and their pictures. This example uses
       the Django web framework, but should be adaptable to others."""

    # Get api_key and secret_key from a file
    fb_file = open('facebook_keys.txt').readlines()
    api_key = fb_file[0].strip()
    secret_key = fb_file[1].strip()
    fb = WebAppWidget(api_key, secret_key)

    # Use the data from the cookie if present
    if 'session_key' in request.session and 'uid' in request.session:
        fb.session_key = request.session['session_key']
        fb.uid = request.session['uid']
    else:

        try:
            fb.auth_token = request.GET['auth_token']
        except KeyError:
            # Send user to the WebAppWidget to login
            return HttpResponseRedirect(fb.get_login_url())

        # getSession sets the session_key and uid
        # Store these in the cookie so we don't have to get them again
        fb.auth_getSession()
        request.session['session_key'] = fb.session_key
        request.session['uid'] = fb.uid

    try:
        friend_ids = fb.friends_get()
    except WebAppWidgetError, e:
        # Error 102 means the session has expired.
        # Delete the cookie and send the user to WebAppWidget to login
        if e.info['code'] == u'102':
            del request.session['session_key']
            del request.session['uid']
            return HttpResponseRedirect(fb.get_login_url())
        else:
            # Other WebAppWidget errors are possible too. Don't ignore
them.
            raise

    info = fb.users_getInfo(friend_ids, ['name', 'pic'])
    # info is a list of dictionaries

    # you would never do this in an actual Django application,
    # it's just an example of accessing the results.
    links = []
    for friend in info:
        html = '<a href="%(pic)s">%(name)s</a>' % friend
        links.append(html)

    return render_to_response('template.html', {'links': links})
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061224/d3a0559f/attachment-0001.htm 


More information about the Tutor mailing list