Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically getting an access token for using the Facebook Graph API

I am trying to put together a bash or python script to play with the facebook graph API. Using the API looks simple, but I'm having trouble setting up curl in my bash script to call authorize and access_token. Does anyone have a working example?

like image 673
justinhj Avatar asked Jun 17 '10 03:06

justinhj


People also ask

How do I get an access token for Microsoft Graph API?

To get an access token, your app must be registered with the Microsoft identity platform and be authorized by either a user or an administrator to access the Microsoft Graph resources it needs. This article provides an overview of the Microsoft identity platform, access tokens, and how your app can get access tokens.

How do I get my Facebook client token?

You can learn more about obtaining a user access token by implementing Facebook Login for Android. You can retrieve the user access token by inspecting Session. getCurrentAccessToken .

How do I get data from Facebook Graph API?

Open the Graph Explorer in a new browser window. This allows you to execute the examples as you read this tutorial. The explorer loads with a default query with the GET method, the lastest version of the Graph API, the /me node and the id and name fields in the Query String Field, and your Facebook App.


1 Answers

Update 2018-08-23

Since this still gets some views and upvotes I just want to mention that by now there seems to exist a maintained 3rd party SDK: https://github.com/mobolic/facebook-sdk


Better late than never, maybe others searching for that will find it. I got it working with Python 2.6 on a MacBook.

This requires you to have

  • the Python facebook module installed: https://github.com/pythonforfacebook/facebook-sdk,
  • an actual Facebook app set up
  • and the profile you want to post to must have granted proper permissions to allow all the different stuff like reading and writing.

You can read about the authentication stuff in the Facebook developer documentation. See https://developers.facebook.com/docs/authentication/ for details.

This blog post might also help with this: http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/

Here goes:

#!/usr/bin/python # coding: utf-8  import facebook import urllib import urlparse import subprocess import warnings  # Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError). warnings.filterwarnings('ignore', category=DeprecationWarning)   # Parameters of your app and the id of the profile you want to mess with. FACEBOOK_APP_ID     = 'XXXXXXXXXXXXXXX' FACEBOOK_APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' FACEBOOK_PROFILE_ID = 'XXXXXX'   # Trying to get an access token. Very awkward. oauth_args = dict(client_id     = FACEBOOK_APP_ID,                   client_secret = FACEBOOK_APP_SECRET,                   grant_type    = 'client_credentials') oauth_curl_cmd = ['curl',                   'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)] oauth_response = subprocess.Popen(oauth_curl_cmd,                                   stdout = subprocess.PIPE,                                   stderr = subprocess.PIPE).communicate()[0]  try:     oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0] except KeyError:     print('Unable to grab an access token!')     exit()  facebook_graph = facebook.GraphAPI(oauth_access_token)   # Try to post something on the wall. try:     fb_response = facebook_graph.put_wall_post('Hello from Python', \                                                profile_id = FACEBOOK_PROFILE_ID)     print fb_response except facebook.GraphAPIError as e:     print 'Something went wrong:', e.type, e.message 

Error checking on getting the token might be better but you get the idea of what to do.

like image 70
maryisdead Avatar answered Oct 14 '22 15:10

maryisdead