googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

google-api-php-client not able to fetchAccessToken #2484

Closed vitoff34 closed 11 months ago

vitoff34 commented 11 months ago

Hello, I am trying to implement google-api-php-client in my php application so I can use social login. I am building the project with Symfony 5.4 on PHP 8.0. I am still in local development, using localhost on MACOS. I installed the latest version of the package 2.15. I am able to reach the point where the user is prompted to select a google account for authentication. However when he clicks on the account the page starts loading and keeps doing that forever without any result.

This is my code for generating the auth url:

        $client = new Client();
        $client->setClientId($_ENV['GOOGLE_CLIENT_ID']);
        $client->setClientSecret($_ENV['GOOGLE_SECRET']);
        $client->addScope(['https://www.googleapis.com/auth/userinfo.profile']);
        $redirectUrl = $generator->generate('connect_google_check', [], UrlGeneratorInterface::ABSOLUTE_URL);
        $client->setRedirectUri($redirectUrl);
        $client->setIncludeGrantedScopes(true);

        $authUrl = $client->createAuthUrl();
        return $this->redirect($authUrl);

And this is my code for authenticating in a different controller:

        $client = new Client();
        $client->setClientId($_ENV['GOOGLE_CLIENT_ID']);
        $client->setClientSecret($_ENV['GOOGLE_SECRET']);
        $client->addScope(['https://www.googleapis.com/auth/userinfo.profile']);
        $client->setRedirectUri('http://myapp.com/connect/google/check');
        $client->setIncludeGrantedScopes(true);
        $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();

        $client->fetchAccessTokenWithAuthCode($request->query->get('code'), $_SESSION['code_verifier']);

I have also tried to use the authenticate method instead of fetchAccessTokenWithAuthCode.

Once the code reaches fetchAccessTokenWithAuthCode() or authenticate() it gets stuck.

Any idea what could be the problem?

I have tried other bundles that implement google oauth2 and the same problem occurs.

I am using a VPN, no idea if this has anything to do with it?