Yii2 authclient does not return user email id
I am using yii2-authclient to facebook login for my site, I get a 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' => '****',
],
],
]
Is it possible to get the username, user id, but not the user's email address? What else should I have done?
source to share
Yes finally got a solution,
when you make a graph api request
$this->makeSignedRequest('me');
replace it with
$this->makeSignedRequest('me?fields=id,name,email');
either you use with facebook sdk or you use the yii2-eauth extension like me. if you are using yii2-eauth then you need to change this in vendor / nodge / yii 2-eauth / src / services / FacebookOAuth2Service.php
then add this line to read email attribute
$this->attributes['email'] = $info['email'];
Note. I have tried yii argument passing path like
$this->makeSignedRequest('me',array('fields'=>'id,email');
didn't work for me.
Solution source: https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile/5.0.0
source to share
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).
source to share
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => '****',
'clientSecret' => '*****',
],
It's enough. But sometimes you still can't get the email address because some facebook user doesn't have an email address:
- Facebook user registration by phone
- Facebook user registration via email but email is not confirmed
You have to catch it to notify the user.
I don't know of any other exceptions. if the user has an email but your app is unable to receive that email, let me check the email
permission listed Approved Items
on the status and view page on facebook developer to see why.
https://developers.facebook.com/apps/YOUR_APP_ID/review-status/
source to share