Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Facebook access token using Python library?

I've found this Library it seems it is the official one, then found this, but everytime i find an answer the half of it are links to Facebook API Documentation which talks about Javascript or PHP and how to extract it from links!

How do i make it on simple python script?

NB: what i realy dont understand, why using a library and cant extract token if we can use urllib and regex to extract informations?

like image 471
Abdelouahab Avatar asked May 24 '13 16:05

Abdelouahab


Video Answer


3 Answers

Not sure if this helps anyone, but I was able to get an oauth_access_token by following this code.

from facepy import utils
app_id = 134134134134 # must be integer
app_secret = "XXXXXXXXXXXXXXXXXX" 
oath_access_token = utils.get_application_access_token(app_id, app_secret)

Hope this helps.

like image 120
lemonpro Avatar answered Oct 30 '22 23:10

lemonpro


Javascript and PHP can be used as web development languages. You need a web front end for the user to grant permission so that you can obtain the access token.

Rephrased: You cannot obtain the access token programmatically, there must be manual user interaction

In Python it will involve setting up a web server, for example a script to update feed using facepy

import web
from facepy import GraphAPI
from urlparse import parse_qs

url = ('/', 'index')

app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"

user_data = web.input(code=None)

if not user_data.code:
    dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
                               "client_id=" + app_id +
                               "&redirect_uri=" + post_login_url +
                               "&scope=publish_stream" )

    return "<script>top.location.href='" + dialog_url + "'</script>"
else:
    graph = GraphAPI()
    response = graph.get(
        path='oauth/access_token',
        client_id=app_id,
        client_secret=app_secret,
        redirect_uri=post_login_url,
        code=code
    )
    data = parse_qs(response)
    graph = GraphAPI(data['access_token'][0])
    graph.post(path = 'me/feed', message = 'Your message here')
like image 28
phwd Avatar answered Oct 30 '22 23:10

phwd


here is a Gist i tried to make using Tornado since the answer uses web.py

https://gist.github.com/abdelouahabb/5647185

like image 20
Abdelouahab Avatar answered Oct 30 '22 23:10

Abdelouahab