Random ...
 
October 2017
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Tags ...
Links



python+zope++:: gae + python + facebook = friends indeed
Posted at 15.May,2009 09:56  Comments 3 / Trackbacks 0 / Like this post!
Technorati tag(s):

Facebook connect lets our users log in and interact with our site using their facebook accounts.

With facebook APIs, we can then do more stuff such as publishing their actions at our sites to facebook; be more social.

We've looked at opensocial, and thought that facebook connect is another option to tap.

Here's how we put together google app engine, python+pyfacebook and facebook connect together.

Get google app engine sdk and read the tutorial. We'll be using the guestbook sample.

Also, create a new facebook application (via facebook developer app), and get the api_key and secret.

Here's the main class:

 # -*- coding: utf-8 -*-
 import cgi, os
 import datetime, Cookie, md5
 import wsgiref.handlers
 import facebook

 from google.appengine.ext import db
 from google.appengine.api import users
 from google.appengine.ext import webapp
 from google.appengine.ext.webapp import template

 api_key = 'fbook app code'
 secret_key = 'fbook app secret'

 class Greeting(db.Model):
    author = db.StringProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)

 class MainHandler(webapp.RequestHandler):

    def get(self):
        greetings_q = Greeting.all().order('-date')
        greetings = greetings_q.fetch(10)
        template_values = {'greetings': greetings}
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

 class MembersHandler(webapp.RequestHandler):
     def get(self):   #get requests
     #instantiate our pyfacebook
        self.facebookapi = facebook.Facebook(api_key, secret_key) 
        #check for md5 values (cookie & calculated value) for verification
        fbcookies = self.facebookapi.validate_cookie_signature(self.request.cookies) 
        if fbcookies is not None: 
        #get fb user
            user = self.facebookapi.users.getInfo( [fbcookies['user']], ['uid', 'name', 'profile_url', 'relationship_status'])[0]

        greetings_q = Greeting.all().order('-date')
        greetings = greetings_q.fetch(10)
        template_values = {'greetings': greetings,
                            'user': user,
                            }
        path = os.path.join(os.path.dirname(__file__), 'members.html')
        self.response.out.write(template.render(path, template_values))

 class Guestbook(webapp.RequestHandler):
    def post(self):
        self.facebookapi = facebook.Facebook(api_key, secret_key)
        fbcookies = self.facebookapi.validate_cookie_signature(self.request.cookies)
        if fbcookies is not None:
            user = self.facebookapi.users.getInfo( [fbcookies['user']], ['uid', 'name', 'profile_url', 'relationship_status'])[0]
            greeting = Greeting()
            greeting.author = user['name']
            greeting.content = self.request.get('content')
            greeting.put()
        else:
            pass
        self.redirect('/')

 def main():
    application = webapp.WSGIApplication([('/', MainHandler), ('/members.html', MembersHandler),
        ('/sign',Guestbook)
        ],
          debug=True)
    wsgiref.handlers.CGIHandler().run(application)

 if __name__ == '__main__':
    main()

Note the guestbook model where we will use user's facebook identity instead of google's.

Facebook sessions can be transferred from Javascript to server side codes via cookies. Look for cookies in the form of api_key_xxx, where xxx maybe user, ss, etc.

In the above code, the MembersHandler class is interesting since that's where much wrangling is done.

There's code duplication (when instantiating pyfacebook) in different classes. There should be a better way. Ideas?

We learned a lot (the session transfer, for one) during this experience and hope to try and do something similar with opensocial.

To work with facebook openapi stream, see the pyfacebook patch in gwibber.

Go see the demo. Leave some note behind!

Helpful resources:

    http://wiki.developers.facebook.com/index.php/Facebook_Connect
    http://bazaar.launchpad.net/~segphault/gwibber/template-facebook-stream/annotate/head%3A/gwibber/microblog/facebook.py
    http://dollarmani-facebook.blogspot.com/2008/09/using-facebook-api-in-python.html
    http://facebooktoolkit.codeplex.com/Thread/View.aspx?ThreadId=40599&ANCHOR


Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/236/tbping 

Comments

The facebook.py you link to is a different version than what you use.

Abhishek  at 16.May,2011 14:35
indeed different

it *has* been a while. fb now has their own py lib.

should i update this tut?

kedai  at 21.May,2011 15:39

Assalamualaikum Salam sejahtera untuk kita semua, Sengaja ingin menulis sedikit kesaksian untuk berbagi, barangkali ada teman-teman yang sedang kesulitan masalah keuangan,SAYA IBU DEWI, sekeluarga mengucapkan banyak terimakasih kepada AKI JOYO MALIK atas bantuannya saya menang togel yang ke 3x nya ,pekerjaan saya sehari-harinya cuma seorang pengepul barang bekas apalagi saya seorang janda,,yang pendapatannya tidak seberapa,buat biaya anak sekolah aja tidak cukup apalagi untuk mencukupi kebutuhan hidup keluarga sehari-harinya….pada su atu hari saya tidak sengaja mendengar pembicaraan teman saya mengenai angka ritual/ghoib AKI JOYO MALIK yang katanya bisa mengeluarkan angka sgp/hk yang di jamin tembus,akhirnya saya bertanya dan teman saya memberikan nomor AKI JOYO MALIK dan saya pun menghubunginya..?? Berkat bantuan AKI yang telah memberikan angka “GHOIB” nya 4D dan alhamdulillah itu ternyata terbukti. lagi…sekarang anak saya bisa lanjut sekolah lagi itu semua atas berkat bantuan AKI JOYO MALIK bagi anda yang penggemar togel ingin meruban nasib melalui angka2 goib yang di jamin 100% kemenangan hbg AKI JOYO MALIK di nmr;_ 085-211-977-346,ini bukti nyata bukan rekayasa,mana ada kemenangan tanpa keberanian dan kejujuran,saatnya kita perlu bukti bukan sekedar janji2,hanya AKI JOYO MALIK yang bisa menjamin 100% kesuksesan,anda perlu bukti siahkan HBG/SMS AKI JOYO MALIK nya,terima kasih ROMNYA

INGAT…!!! JANGAN SIA-SIAKAN KESEMPATAN YANG ADA SEBAB

KESEMPATAN TIDAK MUNGKIN DATANG KE 2 KALINYA………

mbakdewi  at 23.Apr,2017 18:43
Post a comment