首页 > 解决方案 > 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('/');
    }

标签: laravelgoogle-api

解决方案


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

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

推荐阅读