yii2yii-componentsyii2-advanced-app

Yii2 authclient not returning email id of user


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?


Solution

  • 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