拉威尔社交名媛-Facebook没有';t返回用户';s firstname&;姓氏
我正在使用laravel Socialite创建facebook登录 这是我的提供者重定向拉威尔社交名媛-Facebook没有';t返回用户';s firstname&;姓氏,facebook,laravel,laravel-5.4,laravel-socialite,Facebook,Laravel,Laravel 5.4,Laravel Socialite,我正在使用laravel Socialite创建facebook登录 这是我的提供者重定向 public function redirectToProvider($provider = null) { if (!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist if($provider == "facebook"){
public function redirectToProvider($provider = null)
{
if (!config("services.$provider"))
abort('404'); //just to handle providers that doesn't exist
if($provider == "facebook"){
return Socialite::driver('facebook')
->fields(['first_name', 'last_name', 'email', 'gender', 'birthday'])
->scopes(['email', 'user_birthday'])->redirect();
}
}
但是,在回调中,我转储$user以查看接收到哪些数据
public function findOrCreateUser($user, $provider)
{
$authUser = User::where('provider_id', $user->id)->first();
if ($authUser) {
return $authUser;
}
switch ($provider) {
case "facebook":
var_dump($user);
return User::create([
'firstname' => $user->user['first_name'],
'lastname' => $user->user['last_name'],
'email' => $user->user['email'],
'email_verified' => true,
'provider' => $provider,
'provider_id' => $user->user['id']
]);
break;
case "google":
return User::create([
'firstname' => $user['name']['givenName'],
'lastname' => $user['name']['familyName'],
'email' => $user['email'],
'email_verified' => true,
'provider' => $provider,
'provider_id' => $user['id']
]);
break;
}
}
这是我从facebook收到的数据
object(Laravel\Socialite\Two\User)[215]
public 'token' => string '******' (length=178)
public 'refreshToken' => *****
public 'expiresIn' => int ******
public 'id' => string '*****' (length=15)
public 'nickname' => null
public 'name' => string '******' (length=15)
public 'email' => string '*****' (length=24)
public 'avatar' => string '******' (length=67)
public 'user' =>
array (size=6)
'name' => string '*****' (length=15)
'email' => string '*****' (length=24)
'gender' => string '****' (length=4)
'verified' => boolean ****
'link' => string '*****' (length=60)
'id' => string '****' (length=15)
public 'avatar_original' => string '****' (length=66)
public 'profileUrl' => string '*****' (length=60)
正如你所能看到的,我没有收到我要求的所有数据,我也不知道为什么
如何确保facebook向我提供用户的
名字和姓氏 这是我的工作代码:
try
{
$socialUser = Socialite::driver('facebook')->fields(['name', 'first_name', 'last_name', 'email'])->user();
}
catch (Exception $e)
{
\Log::error($e);
return redirect()->route('register')->with('error', 'ERROR');
}
$user = new \StdClass;
$user->id = $socialUser->id;
$user->name = $socialUser->name;
$user->email = $socialUser->email;
$user->firstname = $socialUser->user['first_name'];
$user->lastname = $socialUser->user['last_name'];
尝试删除->作用域(['email','user_birth'])
或用我的代码替换它
编辑:我想我也遇到了同样的问题,->user()
就是解决方案