Chris
Chris

Reputation: 2446

HWIOauth & Facebook access user_friends

I am trying to access the user_friends sent by facebook after oauth using HWIOauthBundle.

##config.yml
    resource_owners:
            facebook:
                type:                facebook
                client_id:           %facebook_app_id%
                client_secret:       %facebook_app_secret%
                scope:               "email, public_profile, user_friends"
                infos_url:     "https://graph.facebook.com/me?fields=id,name,email,picture.type(square)"
                paths:
                    email:          email
                    profilepicture: picture.data.url

In the loadUserByOAuthUserResponse(UserResponseInterface $response)

i am doing this to log the data:

$attr = $response->getResponse();
$this->logger->info(print_r($attr,true));

and I get an output array with the info:

Array
(
    [id] => 1015539##########
    [name] => john smith
    [email] => [email protected]
    [picture] => Array
        (
            [data] => Array
                (
                    [is_silhouette] =>
                    [url] => https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xaf1/v/t1.0-1/p50x50/10998###_1015530##########_207119375##########_n.jpg?oh=1f0e94af68daa############f888611&oe=5#######&__gda__=1###720385_a2c0bb15f160abf4############3289
                )
        )
)

(#### and fake name added for privacy)

How do I access the user_friends?

I believe it is working on facebook's end as it asks for permission when logging in.

Upvotes: 0

Views: 769

Answers (2)

Petre
Petre

Reputation: 108

I've done it by adding in my config.yml:

resource_owners:
   facebook:
        type:                facebook
        client_id:           "%fb_app_id%"
        client_secret:       "%fb_app_secret%"
        scope:               "email,user_friends"   
        infos_url:           "https://graph.facebook.com/me?fields=id,name,email,picture.type(square),friends"
        options:
            display: page
        paths:
          friends:  friends 
          profilepicture: picture.data.url

Now in the loadUserByOAuthUserResponse in FOSUBUserProvider you can get from response friends data. SDK is unnecessary.

Upvotes: 1

cilantro
cilantro

Reputation: 548

You are on the right track. You got the user_friends permission while logging your user in.

The next step is to make a Graph API Call to the /me/friends edge (using the person's currently obtained access token) and parse the response.

I don't know if you have the Facebook SDK integrated into your PHP app, but if so, all you need to do this:

/* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
  $session,
  'GET',
  '/me/friends'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */

Alternatively you could do a curl call. For more information on how to do this check https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends

Upvotes: 1

Related Questions