0

How do I get the public profile data of the users who have at one time logged into my web app through facebook? I have old access tokens, email addresses, and ids for each of these users. Many of them have made comments and/or posts on our site and I would like to display their facebook profile next to their comment. (Similar to how it's done at the bottom of this article except that the post was made in our web application and will never show up on Facebook.) I can get the url for the photo just fine like this:

$fb_photo_json = file_get_contents(https://graph.facebook.com/[facebook user id here]/picture?redirect=false);
$fb_photo_data = json_decode($fb_photo_json);
$fb_profile_img_url = $fb_photo_data->data->url;

I tried a similar method like this to get the name of the facebook user:

$fb_data_json = file_get_contents('https://graph.facebook.com/[facebook user id here]?fields=first_name,last_name');

but I always get this error:

{
   "error": {
      "message": "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
      "type": "GraphMethodException",
      "code": 100
   }
}

This is a frustrating error! I've read a good portion of Facebook's API documentation and I've found it to be dense and largely unhelpful. Do I need a separate access token each time I request this data as is suggested here? If so, this seems prohibitive because that would be a lot of external requests. (We have lots of posts from different users.)

UPDATE

I've been messing around with Facebook's Graph API Explorer and I get the same error as above when the "Application" is selected as "Graph API Explorer". But when I change the name of the "Application" to my web app, then click "Get Access Token", and enter the Facebook ID of one of my users in the GET field, it works! I can now get the public profile information of any user that has logged into my site through Facebook. This must mean that all I need to do is send my app's access token along with the request. Not sure how to do that but I will post it as soon as I figure it out... tomorrow (I'm sleepy).

Community
  • 1
  • 1
sterfry68
  • 1,063
  • 2
  • 14
  • 30
  • If you're using any of Facebook's SDKs the access token should be sent with each request implicitly (assuming you've implemented login correctly) - otherwise, if you're manually calling the API, you need to add the access token as a parameter yourself – Igy Sep 12 '14 at 05:26
  • Not using Facebook's SDK. I'm using a package written specifically for Laravel. In the process of figuring out how to send the access token as a parameter. I'm new to http get requests. – sterfry68 Sep 12 '14 at 15:27

1 Answers1

0

This works:

$fb_user_info = file_get_contents('https://graph.facebook.com/{users-fb-id}?fields=first_name,last_name&access_token={my-apps-access-token}');

Can get the access token this way:

$app_token = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials');
sterfry68
  • 1,063
  • 2
  • 14
  • 30