Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Facebook Javascript SDK to get Graph Data

FIXED NOW! But I can't answer my own question yet. See my comment below. And thanks for helping.


I've searched and searched and read the docs and still can't figure this out.

I have a web page about an event. There's also a public Facebook "event" for my event. I'm trying to use the FB Javascript SDK to get the number of attendees for the Facebook event and add it to the number of people who've registered through the website.

I've created an app and I have an appID and secret string. I can get an access token from:

https://graph.facebook.com/oauth/access_token?client_id=XXXX&client_secret=XXXXX&grant_type=client_credentials

and I can then use that access token to get the attendees for a public event:

https://graph.facebook.com/331218348435/attending?access_token=XXXXXXXXX

That's all fine.

I'm now trying to do this same thing using the Javascript SDK.

I've loaded the SDK and done an init:

FB.init({
  appId  : 'XXXXXXXX',
  status : true, // check login status
  cookie : true, // enable cookies to allow the server to access the session
  xfbml  : true  // parse XFBML
});

and I know the SDK is working because I can get an object with the data that doesn't need an access token:

FB.api( '/331218348435', function (response) { console.log ( response ) } );

but when I try to get the attendee data that needs the access token:

FB.api( '/331218348435/attending', function (response) { console.log ( response ) } );

I get an OAuthException: "An access token is required to request this resource."

All the tutorials and information I can find all refers to using the .login method, but I don't want a user to login, I want to login using my app ID without any user interaction.

I'd assumed that the API took the SDK's init request and granted me an access token when I called the .init method, the authentication being done against my website's address (the HTTP referrer - yes I have set my website URL in the Facebook app settings).

Any ideas what might be causing this to not work? How can I get the access token using the Javascript SDK without doing a .login? Am I missing a step? Is this even possible?

Thanks

like image 297
magicroundabout Avatar asked Jul 08 '11 16:07

magicroundabout


1 Answers

Form what the rather circular documentations says, getting the attending feed requires a 'generic access_token`. In Facebook terms:

Any valid access_token Any valid access token returned by our APIs. An access token may not be valid if, for example, it has expired. No special permissions are required. Occasionally, this is referred to as a generic access_token.

So this means that you can use any token you like to access the attending feed, as long as the event is public. The easiest access token to get seems to be an app token: http://developers.facebook.com/docs/authentication/#applogin. You can get this token using only your App ID and Secret, and no user interaction is required.

To summerise the link content: You can get an application access token by sending a GET request to

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials 

You can then use that access_token to make the call for you attending list

FB.api('MyEvent/attending?access_token=ACCESS_TOKEN');
like image 66
shanethehat Avatar answered Sep 29 '22 03:09

shanethehat