【问题标题】:ClientException Client error: `GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false` resulted in a `403 Forbidden` response:ClientException 客户端错误:`GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false` 导致 `403 Forbidden` 响应:
【发布时间】:2020-11-16 00:21:12
【问题描述】:

当我想用谷歌帐户登录时,我收到了这个错误,

ClientException 客户端错误:GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false 导致403 Forbidden 响应:

 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('/');
    }

【问题讨论】:

  • 您是否正确设置了身份验证凭据,您发送的凭据似乎未通过 google apis 验证为正确
  • 在博客中显示 this,为您工作
  • 非常感谢,我使用了这个网站并像这样在GoogleProvider.php中更改了我的代码,绿色句子是正确的github.com/laravel/socialite/pull/283/files
  • 您可以在答案的步骤中发布为您解决的问题,以便任何来这里寻求帮助的人都可以关注您的答案

标签: laravel google-api


【解决方案1】:

据我所知,您需要传递一个令牌。

https://laravel.com/docs/master/socialite#retrieving-user-details

【讨论】:

    【解决方案2】:

    这是解决此问题的方法。我是从这个网站上找到的。

    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'),
            ]);
        }
    

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      相关资源
      最近更新 更多