Code Monkey home page Code Monkey logo

facebook's Introduction

Facebook

Python wrapper for Facebook Graph API

Features

  • Authentification
  • Graph API supprt

Installation

Install via pip

pip install FacebookGraph

or, with easy_install

easy_install FacebookGraph

Or, if you want the code that is currently on GitHub

git clone git://github.com/BUHARDI/facebook.git
cd facebook
python setup.py install

Starting Out

Register an application https://developers.facebook.com/apps

After you have registered the application, copy and save ID and API SECRET. Example below shows that I have stored those variables in Django settings as FACEBOOK_ID and FACEBOOK_SECRET.

Now you're ready to use Facebook Graph API. Import the wrapper class.

from facebook import Facebook

Authentification

This example is based on Django framework

Example shows how to get Facebook user data. You only need to do this, if you created an application of type "Website with Facebook Login".

Typically this would be the process starting view.

from django.conf import settings

def login_facebook(request):
    facebook = Facebook(settings.FACEBOOK_ID, settings.FACEBOOK_SECRET)
    callback_url = 'http://domain.tld/login/facebook/callback'
    login_url = facebook.get_login_url(redirect_uri=callback_url) # you can pass extra keyword arguments to the login url, like scope='publish_stream,email' or display='popup'
    return redirect(login_url)

callback_url is url where user will be redirected after login on Facebook. In this view you will receive extra GET variables to complete the autorization.

Last things to end the login process - callback view. Read the documentation.

def login_facebook_callback(request):
    if not request.GET.get('code'):
	    return HttpResponseBadRequest()
    facebook = Facebook(settings.FACEBOOK_ID, settings.FACEBOOK_SECRET)
    token = facebook.code2token(request.GET['code'], callback_url)
    me = facebook.api('/me')

Thats it! me variable contains dictionary of user profile.

It's good idea store the user token in case you want use API again later. In Django you can take advantage of sessions.

request.session['facebook_token'] = token

To use API, you have to set user token.

facebook.set_access_token(request.session['facebook_token'])

Using the API

Chekout the code and the documentation. You should be ready to dive in by now.

facebook = Facebook(settings.FACEBOOK_ID, settings.FACEBOOK_SECRET, request.session['facebook_token'])

Create an instance. Third argument is user token which you received after user authorization.

More examples:

me = facebook.api('/me')

feed = facebook.api('/kalnins.marcis/feed')

post = facebook.api('/me/feed', 'POST', message='My test message!')

facebook's People

Contributors

marcispsc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.