File: facebook.rst

package info (click to toggle)
python-requests-oauthlib 1.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 496 kB
  • sloc: python: 2,000; makefile: 159
file content (39 lines) | stat: -rw-r--r-- 1,741 bytes parent folder | download | duplicates (7)
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
32
33
34
35
36
37
38
39
Facebook OAuth 2 Tutorial
=========================

Setup a new web application client in the `Facebook APP console`_
When you have obtained a ``client_id``, ``client_secret`` and registered
a callback URL then you can try out the command line interactive example below.

.. _`Facebook APP console`: https://developers.facebook.com/apps

.. code-block:: pycon

    >>> # Credentials you get from registering a new application
    >>> client_id = '<the id you get from facebook>'
    >>> client_secret = '<the secret you get from facebook>'

    >>> # OAuth endpoints given in the Facebook API documentation
    >>> authorization_base_url = 'https://www.facebook.com/dialog/oauth'
    >>> token_url = 'https://graph.facebook.com/oauth/access_token'
    >>> redirect_uri = 'https://localhost/'     # Should match Site URL

    >>> from requests_oauthlib import OAuth2Session
    >>> from requests_oauthlib.compliance_fixes import facebook_compliance_fix
    >>> facebook = OAuth2Session(client_id, redirect_uri=redirect_uri)
    >>> facebook = facebook_compliance_fix(facebook)

    >>> # Redirect user to Facebook for authorization
    >>> authorization_url, state = facebook.authorization_url(authorization_base_url)
    >>> print 'Please go here and authorize,', authorization_url

    >>> # Get the authorization verifier code from the callback url
    >>> redirect_response = raw_input('Paste the full redirect URL here:')

    >>> # Fetch the access token
    >>> facebook.fetch_token(token_url, client_secret=client_secret,
    ..>                      authorization_response=redirect_response)

    >>> # Fetch a protected resource, i.e. user profile
    >>> r = facebook.get('https://graph.facebook.com/me?')
    >>> print r.content