laravel - ClientException 客户端错误:`GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false` 导致 `403 Forbidden` 响应:
问题描述
当我想用谷歌帐户登录时,我收到这个错误,
ClientException 客户端错误:
GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false
导致403 Forbidden
响应:<meta name=viewport content="initial-scale=1, minimum-scale=1, w (truncated...) in RequestException.php line 113
public function redirectToProvider()
{
return Socialite::driver('google')->redirect();
}
public function handleProviderCallback()
{
$social_user = Socialite::driver('google')->user();
$user = User::whereEmail($social_user->getEmail())->first();
if( ! $user ) {
$user = User::create([
'name' => $social_user->getName(),
'email' => $social_user->getEmail(),
'password' => bcrypt($social_user->getId())
]);
}
if($user->active == 0) {
$user->update([
'active' => 1
]);
}
auth()->loginUsingId($user->id);
return redirect('/');
}
解决方案
这是这个问题的解决方案。我是从这个网站上找到的。
https://github.com/laravel/socialite/pull/283/files 我在我的项目中更新了 GoogleProvider.php。评论语句必须更新。
GoogleProvider.php:
protected function getUserByToken($token)
{
// $response = $this->getHttpClient()->get('https://www.googleapis.com/plus/v1/people/me?', [
$response = $this->getHttpClient()->get('https://www.googleapis.com/userinfo/v2/me?', [
'query' => [
'prettyPrint' => 'false',
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
// 'id' => $user['id'], 'nickname' => array_get($user, 'nickname'), 'name' => $user['display Name'],
// 'email' => $user['emails'][0]['value'], 'avatar' => array_get($user, 'image')['url'],
'id' => $user['id'], 'nickname' => array_get($user, 'nickname'), 'name' => $user['name'],
'email' => $user['email'], 'avatar' => array_get($user, 'picture'),
]);
}
推荐阅读
- c# - Asp.net Web 应用程序中的音频和视频聊天
- python - 从 ACI 迁移到 AKS 部署的最简单方法是什么?
- python-2.7 - 无法使用子进程执行 py 文件
- iframe - 如何在 iframe 中拖动滑块?
- php - 来自 Instagram API 的 json_decode 非常慢
- html - 删除溢出自动 div 下的空白
- regex - 正则表达式检查字符串的起始字符和字符串的长度
- arrays - 根据条件和数组元素位置循环遍历postgresql表中的数组返回
- c++ - const 自动引用绑定到(空)指针 - 实际类型是什么?
- javascript - 在反应函数中更新后从文本输入中获取值