3

Am using yii2-authclient for facebook login for my site, Am getting response like this

yii\authclient\clients\Facebook Object ( [authUrl] => https://www.facebook.com/dialog/oauth?display=popup [tokenUrl] => https://graph.facebook.com/oauth/access_token [apiBaseUrl] => https://graph.facebook.com [scope] => email [version] => 2.0 [clientId] => *******[clientSecret] => ********[_returnUrl:yii\authclient\BaseOAuth:private] => http://www.usermodule.com/index.php?r=site%2Fauth&authclient=facebook [_curlOptions:yii\authclient\BaseOAuth:private] => Array ( ) [_accessToken:yii\authclient\BaseOAuth:private] => yii\authclient\OAuthToken Object ( [tokenParamKey] => access_token [tokenSecretParamKey] => oauth_token_secret [createTimestamp] => 1436772538 [_expireDurationParamKey:yii\authclient\OAuthToken:private] => [_params:yii\authclient\OAuthToken:private] => Array ( [access_token] => ****************************** [expires] => 5182933 ) ) [_signatureMethod:yii\authclient\BaseOAuth:private] => Array ( ) [_id:yii\authclient\BaseClient:private] => facebook [_name:yii\authclient\BaseClient:private] => [_title:yii\authclient\BaseClient:private] => [_userAttributes:yii\authclient\BaseClient:private] => [_normalizeUserAttributeMap:yii\authclient\BaseClient:private] => [_viewOptions:yii\authclient\BaseClient:private] => [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => ) Array ( [name] => **** [id] => *****)

This is my config

 'authClientCollection' => [
    'class' => 'yii\authclient\Collection',
    'clients' => [
        'facebook' => [
            'class' => 'yii\authclient\clients\Facebook',              
            'clientId' => '***',
            'clientSecret' => '****',

        ],
    ],
]

Am able to get username, user id but not user email id why? What else I should do?

user3377747
  • 79
  • 1
  • 9

5 Answers5

3

Yes finally got the solution,

when you make graph api request

$this->makeSignedRequest('me');

replace it with

$this->makeSignedRequest('me?fields=id,name,email');

either you are doing using facebook sdk or using yii2-eauth extension like me. if you are using yii2-eauth then you need to change this in vendor/nodge/yii2-eauth/src/services/FacebookOAuth2Service.php

then add this line to read email attribute

$this->attributes['email'] = $info['email'];

Note: I tried yii way of passing argument like

$this->makeSignedRequest('me',array('fields'=>'id,email');

didn't work for me.

Source of solution: https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile/5.0.0

user3377747
  • 79
  • 1
  • 9
  • easily modificate passing arguments and it'll work `$info = $this->makeSignedRequest('me',array( 'query' => array( 'fields' => 'email, name, birthday, gender, link, first_name, last_name, age_range', ), ));` – Nikolaj Sarry Sep 23 '15 at 18:14
  • when to call $this->makeSignedRequest? sitecontroller? – Robert Limanto Feb 24 '16 at 16:53
2

I got the solution with the help of above solutions.

In yii2-authclient/clients/Facebook.php there is a function initUserAttributes() in which

 return $this->api('me', 'GET');

replace it with

 return $this->api('me?fields=id,name,email', 'GET');
Mohd Bashir
  • 949
  • 1
  • 8
  • 17
1

Really there is some mistake from yii2 eauth extension.

Firstly setting scope params in web.php

            'facebook' => array(
                // register your app here: https://developers.facebook.com/apps/
                'clientId' => '454208034652365',
                'clientSecret' => '0e7161949129d4120cc5ac7d79f89098',
                'class' => 'nodge\eauth\services\FacebookOAuth2Service',
                'scope'        => [
                    'email'
                    ]
            ),

Then can you use like the following

  $info = $eauth->makeSignedRequest('me', [
                'query' => [
                    'fields' => join(',', [
                        'id',
                        'name',
                        'link',
                        'email'
                    ])
                ]
            ]);
            echo $info['email'];
0
    for using Yii2-authclient you have to configure(main.php) like that:-
    inside components array write
    'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [
                'facebook' => [
                    'class' => 'yii\authclient\clients\Facebook',
                    'clientId' => 'your Client-Id',
                    'clientSecret' => 'your Client-secret',
                ],

            ],
        ],
    in controller inside actions method write:-
    auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'successCallback'],
                 ],

then create successCallback function like
public function successCallback($client)
        {
            $attributes= $client->getUserAttributes();
            print_r($attributes);
} 
then you got all attributes which are shared by Facebook(like email, firstname etc).
sprytechies
  • 317
  • 3
  • 7
0
'facebook' => [
    'class' => 'yii\authclient\clients\Facebook',
    'clientId' => '****',
    'clientSecret' => '*****',
 ],

That is enough. But sometime you still can not get email address because some facebook user do not has an email address:

  • facebook user register by phone
  • facebook user register by email but email not verified

You have to catch it to notify the user.

I don't know have any other exceptions. if user have an email but your app can not get that email address, let's check email permission in list Approved Items in "status and review" page on facebook developer to know why. https://developers.facebook.com/apps/YOUR_APP_ID/review-status/

Ngô Văn Thao
  • 3,671
  • 1
  • 20
  • 24
  • I have fb account by email Id, I tested with my friend account also its same no email in response. Approved items email,public profile, user_friends shows approved by default. – user3377747 Jul 13 '15 at 10:14