Skip to main content

Posting to FaceBook feed using Graph API

Graph API was announced at F8 with a promise to dramatically simplify the FB API.
I checked the read access over the new interface during the presentations and to my big surprise it worked flawlessly and from the first time.
When I tried https://graph.facebook.com/facebook, JSON-formatted info about the FaceBook page was returned (as expected).

Then I tried OAuth 2.0 way of accessing the API to post a message to the feed.
And to my even bigger surprise it worked too!

Here is what you need to do to access Graph API over OAuth:
1. Create a FB app, store app properties to a file:

  1. $appkey = '7925873fbfb5347e571744515a9d2804';
  2. $appsecret = 'THE SECRET';
  3. $canvas = 'http://apps.facebook.com/graphapi/';
2. Create a page that will prompt user the access permission (I am prompting for the publish_stream and offline_access permissions at the same time)

  1. //http://apps.facebook.com/graphapi/
  2. require 'settings.php';

  3. $url = "https://graph.facebook.com/oauth/authorize?";
  4. $url .= "client_id=$appid&";
  5. $url .= "redirect_uri=$canvas/callback.php&";
  6. $url .= "scope=publish_stream,offline_access";
  7. $url .= "&type=user_agent&display=popup";

  8. echo("$url");



3. Create a page to handle OAuth call-back with token and do the feed post:

  1. require 'settings.php';

  2. function
    callFb($url, $params) {
  3. $ch = curl_init();
  4. CURLOPT_URL => $url,
  5. CURLOPT_POSTFIELDS => http_build_query($params),
  6. CURLOPT_RETURNTRANSFER => true,
  7. CURLOPT_VERBOSE => true
  8. ));
  9. $result = curl_exec($ch);
  10. curl_close($ch);
  11. return $result;
  12. }

  13. $token = $_REQUEST['access_token'];

  14. $hello = "Hello from Graph API";
  15. $params=array('access_token'=>$token, 'message'=>$hello);
  16. $url = "https://graph.facebook.com/me/feed";
  17. callFb($url, $params);
P.S.


Important! Do not forget to select "new SDK" on the application settings page (I think that Facebook documentation fails to mention that)

Comments

ViN said…
How often do i need to generate an access token? is it a one shot deal?
Unknown said…
With the code show, I request "offline_access" permission from user. if granted token could be used as long as needed (similar to "infinite session key" with old API). Without "offline_access" permission, token will be working for a few hours.
Unknown said…
Not exactly sure why, my response is coming back with a # in my callback instead of a ?. I can easily catch this with php but it seems as if its not exactly working as intended.
Unknown said…
I have a normal response with '?' separating call-back url and returned values. Please make sure that you correct app id matching with app secret. Also you may get more than one call-backs from FaceBook. You can ignore any call-backs after the first one.
Unknown said…
I've verified I have the correct secret but I don't see anywhere in your code where you're using $appsecret. My upfront thought was type was messing with it
CC said…
Looks like you are working on the same stuff I am.

I think you may be omitting some code on that second page; all it does is echo out that URL you've constructed.

I'm trying to get this stuff to work within a Canvas app, and when I redirect the user to that URL you showed, I get Facebook framed with the semitransparent black backdrop. Any idea how to properly get that to work?
Unknown said…
CC, you are absolutely right! This code is just constructing this URL can be constructed manually and user can be redirected to this URL at any point. I even tried to put this URL in app settings so this will be the first page user is redirected to after the app install.
I think for the problem you see, you need to check application properties you set up with FaceBook. I'd pay special attenuation to have your app as FBML, not iframe
CC said…
Thanks for the follow-up. I just now got it working in the canvas context. I added "canvas=1" and "fbconnect=0" to the params, and used "display=page" to get it to work.

Thanks for this post - it saved me a good amount of work!
dennisp said…
Hello,

That's a shame that FB docs are that unclear.

Using your method. It does not return access_token but json-encoded session object. The returned session_key cannot be used with the graph api, just with the older api. Anybody came across this situation? Thanks.
Matt Farnell said…
I get the # instead of a ? as well.

http://www.mydomain.com/callback/#access_token=6477...etc

I have http://www.mydomain.com/callback/ as my redirect_uri.
Unknown said…
I would specify full page URL (as in my original post) for the callback URL to avoid problem with the #
Unknown said…
Hi,

Thanks for the tutorial.. Its gud
I have one issue.. CURL is giving the error, "Could not connect to host".. Donno why.. I have added the IP of graph.facebook.com in hosts file..but still the same..

Is that something to be done with https ..

Please help
Subeesh
Erica said…
Creating a simple post to a fan page through my web site is driving me to drink.

I think I finally have it all pieced together but I am also having the issue with the hash tag "#" separating my callback page and my access token, which google tells me PHP cannot retrieve for me.

my callback page is set to www.mypage.com/admin/facebookpost.php but it goes to www.mypage.com/admin/facebookpost.php#access_token=xxxxxx

I am ready to punch a facebook employee in the brain.

Popular posts from this blog

Facebook Friends Connect

Is a way to extend external sites to provide: FB identity FB friends (relationship) Feed to FB   Demo app at http://www.somethingtoputhere.com/therunaround User experience: login: js login method requiresession(): detects state of usr-FB relationship, log-in into FB if needed. If user has not authorised app - present app auth dialog. If already has session - just go init JS, require session   access FB data: - FBML on external site - use JS FBML parser and replace in browser DOM with FB data - JS based API to get FB data, REST API on the server site. Sessions work accross any API - only small subset of FBML us supported at the moment   adding social content: - use access API   Connections: app developers can suggest connections (using e-mail hash) user get connect request on FB Move content from external sites to FB app can register feed template (3 types of stories) call JS "showfeeddialog" to request user to confirm data sharing on FB. privacy protection: app ca

Ripple Baby Steps

Ripple and XPR What is Ripple for people in business and finance? Ripple is a currency exchange and payment settlement platform built using blockchain technology. Unlike Ethereum that is a more universal distributed application platform, Ripple solves a more narrow set of problems such as sending payments (similar to Bitcoin), currency remittance, payments for invoices, as well as number of other use cases related to payment in different currencies between parties that may or may not trust each other. Ripple is fast, scalable, and provides number of functions needed to support different payment scenarios. XPR is a native Ripple currency with a fixed and limited supply coins. 100 Billion XPR cryptocoins are in circulation today and the same number will be in circulation tomorrow.  What is Ripple for a software engineer?  For a software developer, Ripple is distributed ledger platform accessible trough API. There are number of libraries to accommodate different developers&#