简体   繁体   中英

Laravel 5.2 Socialite Retrieve Null Email

i'm new to Laravel and i'm trying to make a social auth. I have seen some tutorials and I've got to retrieve user id, profile pic and name from Facebook. My problem is the email, it comes null.

My current object is this:

User {#195 ▼
  +token: "EAACYY1xBrzcBAO9KZAjZAZAZBQHWhbtZAb9lIXvH9Y6miRUiABImupk3ZCzHRxy2ZBxc9DU8f1ZAcrw0NEVMmy3GOQMSSyrmDTvMPyREvGm4pA1Ok6zI85LxES4Huy2ZCsxIUr2ISbLreLIv1ZACItUUoqqAnvVPyR4s0ZD"
  +id: "989087311199546"
  +nickname: null
  +name: "MyName"
  +email: null
  +avatar: "https://graph.facebook.com/v2.2/mypicturecode/picture?type=normal"
  +"user": array:2 [▼
    "name" => "MyName"
    "id" => "MyId"
  ]
}

and my controller is:

class FacebookController extends Controller
{
   public function facebook()
    {
        return Socialite::driver('facebook')
            ->scopes(['email'])->redirect();
    }
    public function callback()
    {
        $user = Socialite::with('facebook')->user();

        return dd($user);
        // $user->token;
    }
}

I recently discovered something. I have "guzzlehttp/guzzle": "~4.0" in my composer to avoid the cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html ) error. When the guzzle 4.0 is installed, Laravel Socialite returns to the 2.0.4 version. Looks like the 2.0.18 version deals with the email and first_name/last_name problem. The problem is that when guzzle 4.0 is installed, it requires the socialite 2.0.4

Facebook users can register using a phone number instead of an email address. This means that facebook does not have an email address for that user.

And you might have own privacy policy.

Have a look at this : https://developers.facebook.com/docs/facebook-login/permissions#reference-email

If you are using laravel 5.2 and socialite 2.0 . Then you would face this issue. It's a bug in socialite. Replace it with the code given below . Path : vendor\\laravel\\socialite\\src\\Two\\

https://github.com/laravel/socialite/blob/2.0/src/Two/FacebookProvider.php Then below code would provide you complete details return Socialite::driver('facebook')->fields([ 'first_name', 'last_name', 'email', 'gender', 'birthday' ])->scopes([ 'email', 'user_birthday' ])->redirect();

Just you need to add more permissions to whatever data you want from Facebook object about your user在此处输入图片说明 facebook api url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM