Graph API was announced at F8 with a promise to dramatically simplify the FB API.
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)
Important! Do not forget to select "new SDK" on the application settings page (I think that Facebook documentation fails to mention that)
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:
- $appkey = '7925873fbfb5347e571744515a9d2804';
- $appsecret = 'THE SECRET';
- $canvas = 'http://apps.facebook.com/graphapi/';
- //http://apps.facebook.com/graphapi/
- require 'settings.php';
- $url = "https://graph.facebook.com/oauth/authorize?";
- $url .= "client_id=$appid&";
- $url .= "redirect_uri=$canvas/callback.php&";
- $url .= "scope=publish_stream,offline_access";
- $url .= "&type=user_agent&display=popup";
3. Create a page to handle OAuth call-back with token and do the feed post:
- require 'settings.php';
- functioncallFb($url, $params) {
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_VERBOSE => true
- ));
- return $result;
- }
- $token = $_REQUEST['access_token'];
- $hello = "Hello from Graph API";
- $url = "https://graph.facebook.com/me/feed";
- callFb($url, $params);
Important! Do not forget to select "new SDK" on the application settings page (I think that Facebook documentation fails to mention that)
Comments
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?
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
Thanks for this post - it saved me a good amount of work!
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.
http://www.mydomain.com/callback/#access_token=6477...etc
I have http://www.mydomain.com/callback/ as my redirect_uri.
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
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.