Laravel Facebook-Login / missing email

家住魔仙堡 提交于 2019-12-22 14:05:15

问题


I am trying to create a Facebook-Login in my Laravel application. However, the array I get with all the user information does not contain the users email even though I already ask for permission to receive the email.

This is my code:

Route::get('login/fb', function() {
$facebook = new Facebook(Config::get('facebook'));
$params = array(
    'redirect_uri' => url('/login/fb/callback'),
    'scope' => 'email',
);
return Redirect::to($facebook->getLoginUrl($params));
});

Route::get('login/fb/callback', function() {
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');

$facebook = new Facebook(Config::get('facebook'));



$me = $facebook->api('/me');


return $me;

Returning $me gives me all the important user information besides the email address.

Is there any way to fix this?

Any help would be much appreciated.

Thanks.


回答1:


There are instances that facebook will not return an email. This can be because the user has not set a primary email, or their email has not been validated. In this case, your logic should check to see if an email was returned, if not, use their facebook email. FacebookUsername@facebook.com




回答2:


//I used with sentry

        // get data from input
$code = Input::get( 'code' );

// get fb service
$fb = OAuth::consumer( 'Facebook' );

// check if code is valid

// if code is provided get user data and sign in
if ( !empty( $code ) ) {

    // This was a callback request from facebook, get the token
    $token = $fb->requestAccessToken( $code );

    // Send a request with it
    $result = json_decode($fb->request( '/me?fields=id,name,first_name,last_name,email,photos' ), true);

    $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name']. $result['email'];
    //echo $message. "<br/>";

    //Var_dump
    //display whole array().
    //echo('http://graph.facebook.com/'.$result['id'].'/picture?type=large<br>');
    //dd($result);


    $user = \User::where("email",$result['email'])->first();

    if($user!=NULL){
        $userxx = Sentry::findUserByLogin($result['email']);
        Sentry::login($userxx, false);
    return Redirect::to('Beşiktaş');
    }
    else
        {

            $k=str_random(8);
            $user = Sentry::register(array(
                        'activated' => 1,
                        'facebook' => 1,
                        'password' => $k,
                        'email' => $result['email'],
                        'first_name' =>$result['first_name'],
                        'last_name' => $result['last_name'] ,
                        'avatar' => 'http://graph.facebook.com/'.$result['id'].'/picture?type=large',

                ));

            Sentry::login($user, false);

            return Redirect::to('Beşiktaş');
        } 




}
// if not ask for permission first
else {
    // get fb authorization
    $url = $fb->getAuthorizationUri();

    // return to facebook login url
     return Redirect::to( (string)$url );
}


来源:https://stackoverflow.com/questions/21506327/laravel-facebook-login-missing-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!