6

I have used Laravel 5.4 with socialite 3.0 for social login on my web application. But nowadays I got an error Legacy People API has not been used in project xxx. Then I have made some changes in a core file of socialite package. /vendor/laravel/socialite/src/Two/GoogleProvider.php Line 61: Replace https://www.googleapis.com/plus/v1/people/me? by https://www.googleapis.com/oauth2/v3/userinfo?

And update mapUserToObject function with below code:

protected function mapUserToObject(array $user)
{
    $user['id'] = Arr::get($user, 'sub');
    $user['verified_email'] = Arr::get($user, 'email_verified');
    $user['link'] = Arr::get($user, 'profile');

    return (new User)->setRaw($user)->map([
        'id' => Arr::get($user, 'sub'),
        'nickname' => Arr::get($user, 'nickname'),
        'name' => Arr::get($user, 'name'),
        'email' => Arr::get($user, 'email'),
        'avatar' => $avatarUrl = Arr::get($user, 'picture'),
        'avatar_original' => $avatarUrl,
    ]);
}

4 Answers4

7

This Solution does work. Fixed my issue.

Thanks a lot.

Here is the whole file if anybody is having this issue. Just change GoogleProvider class located in: ./vendor/laravel/socialite/src/Two/GoogleProvider.php with this:

class GoogleProvider extends AbstractProvider implements ProviderInterface{

protected $scopeSeparator = ' ';

/**
 * The scopes being requested.
 *
 * @var array
 */
protected $scopes = [
    'openid',
    'profile',
    'email',
];

/**
 * {@inheritdoc}
 */
protected function getAuthUrl($state)
{
    return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state);
}

/**
 * {@inheritdoc}
 */
protected function getTokenUrl()
{
    return 'https://accounts.google.com/o/oauth2/token';
}

/**
 * Get the POST fields for the token request.
 *
 * @param  string  $code
 * @return array
 */
protected function getTokenFields($code)
{
    return array_add(
        parent::getTokenFields($code), 'grant_type', 'authorization_code'
    );
}

/**
 * {@inheritdoc}
 */
protected function getUserByToken($token)
{
    //fixing legacy google+ api
    $response = $this->getHttpClient()->get('https://www.googleapis.com/oauth2/v3/userinfo?', [
        'query' => [
            'prettyPrint' => 'false',
        ],
        'headers' => [
            'Accept' => 'application/json',
            'Authorization' => 'Bearer '.$token,
        ],
    ]);
    return json_decode($response->getBody(), true);
}

/**
 * {@inheritdoc}
 */
protected function mapUserToObject(array $user)
{
    //fixing legacy google+ api
    $user['id'] = Arr::get($user, 'sub');
    $user['verified_email'] = Arr::get($user, 'email_verified');
    $user['link'] = Arr::get($user, 'profile');

    $avatarUrl = Arr::get($user, 'image.url');
    return (new User)->setRaw($user)->map([
        'id' => Arr::get($user, 'sub'),
        'nickname' => Arr::get($user, 'nickname'),
        'name' => Arr::get($user, 'name'),
        'email' => Arr::get($user, 'email'),
        'avatar' => $avatarUrl = Arr::get($user, 'picture'),
        'avatar_original' => $avatarUrl,
    ]);

}

}

Amir Khalil
  • 187
  • 2
  • 14
  • thanks bro, it works for me.. https://www.googleapis.com/plus/v1/people/me? url with https://www.googleapis.com/oauth2/v3/userinfo? – symi khan Jan 08 '20 at 17:53
  • Update socialite package to [3.3.0](https://github.com/laravel/socialite/releases/tag/v3.3.0) – Empty Brain Aug 31 '20 at 09:50
  • 1
    Been trying to figure this out and came across this and now all is well. On production side there wasn't a problem, but seems google only allows the legacy api endpoint if your app hit that legacy endpoint within 6 months. Since I took a break from developing my site, upon returning I had this problem with legacy! – Brian Aug 30 '21 at 07:46
  • @Brian If you already had the depreciated API working google will allow the API call but if your app hasn't been calling the API in the past they will want you to use the new call – Amir Khalil Oct 04 '21 at 06:47
3

Replace

https://www.googleapis.com/plus/v1/people/me? url with https://www.googleapis.com/oauth2/v3/userinfo?

Google has update the API Endpoint & it recommend to use this https://www.googleapis.com/oauth2/v3/userinfo? endpoint to get the user details.

https://developers.google.com/people/legacy

symi khan
  • 465
  • 5
  • 9
  • 1
    Update socialite package to [3.3.0](https://github.com/laravel/socialite/releases/tag/v3.3.0) – Empty Brain Aug 31 '20 at 09:50
  • @EmptyBrain It's a production server. I don't want to change anything to avoid any complications. For now, this is working fine for me. – Amir Khalil Nov 25 '20 at 23:00
1

I also have Laravel 5.4 and in my case helped upgrade laravel/socialite from 3.0.* to 3.4.

I set this setting in composer: "laravel/socialite": "~3.0" and ran composer update. Don't forget to clear cache: php artisan cache:clear

Sergei Kuraksin
  • 772
  • 9
  • 10
0

The trick here is that it says legacy people api, this often means you are trying to use one of the old Google+ scopes.

A lot of the profile related endpoints that where once part of the google+ api were moved to the People api after the shutdown of Google+ .

As you can see by the endpoint you are calling you are trying to use the old google+ endpoint which was shutdown years ago.

googleapis.com/plus/v1

You should be using people.get

The correct endpoint would be.

GET https://people.googleapis.com/v1/{resourceName=people/*}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449