laravel / socialite

Laravel wrapper around OAuth 1 & OAuth 2 libraries.
https://laravel.com/docs/socialite
MIT License
5.58k stars 940 forks source link

GoogleProvider Type Error #687

Closed alibori closed 9 months ago

alibori commented 9 months ago

Socialite Version

5.11

Laravel Version

9.19

PHP Version

8.1

Database Driver & Version

No response

Description

Hi!

I'm using Socialite for a Flutter mobile app with a Laravel Based backend to sign in with a Google Account.

From frontend I pass to backend the Google access token and the error appears when I try to run userFromToken method of the GoogleProvider class.

In concrete it fails when tries to do json_decode from the response because it's receiving a GuzzleHttp\Psr7\Stream instead of a string:

protected function getUserByToken($token)
  {
      $response = $this->getHttpClient()->get('https://www.googleapis.com/oauth2/v3/userinfo', [
          RequestOptions::QUERY => [
              'prettyPrint' => 'false',
          ],
          RequestOptions::HEADERS => [
              'Accept' => 'application/json',
              'Authorization' => 'Bearer '.$token,
          ],
      ]);

      return json_decode($response->getBody(), true);
  }

If instead, I do this the problem is solved:

protected function getUserByToken($token)
  {
      $response = $this->getHttpClient()->get('https://www.googleapis.com/oauth2/v3/userinfo', [
          RequestOptions::QUERY => [
              'prettyPrint' => 'false',
          ],
          RequestOptions::HEADERS => [
              'Accept' => 'application/json',
              'Authorization' => 'Bearer '.$token,
          ],
      ]);

      return json_decode($response->getBody()->getContents(), true);
  }

Any idea about the cause of this?

Thanks a lot!

Steps To Reproduce

driesvints commented 9 months ago

Strange. Could you send in a PR to check for the type from getBody, then use getContents if it's a PSR7 stream? Thanks!

github-actions[bot] commented 9 months ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

alibori commented 9 months ago

Yes, of course I can!