Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Facebook Graph API from Python using Requests if I do not need user access token?

I am trying to find the easiest way how to use Facebook Graph API using my favorite Requests library. The problem is, all examples I found are about getting user access token, about redirects and user interaction.

All I need is only application access token. I do not handle any non-public data, so I need no user interaction and as my final app is supposed to be command-line script, no redirects are desired.

I found something similar here, but it seems to be everything but elegant. Moreover, I would prefer something using Requests or Requests-OAuth2. Or maybe there is library for that? I found Requests-Facebook and Facepy (both Requests based), but again, all examples are with redirection, etc. Facepy does not handle authorization at all, it just accepts your token and it is up to you to get it somehow.

Could someone, please, provide a short, sane, working example how to get just the application access token?

like image 207
Honza Javorek Avatar asked Jan 30 '13 18:01

Honza Javorek


1 Answers

Following https://developers.facebook.com/docs/technical-guides/opengraph/publishing-with-app-token/:

import requests
r = requests.get('https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=123&client_secret=XXX')
access_token = r.text.split('=')[1]
print access_token

(using the correct values for client_id and client_secret) gives me something that looks like an access token.

like image 88
Richard Barnett Avatar answered Oct 10 '22 08:10

Richard Barnett