Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google does not return refresh token and the token does not refresh automatically #2591

Open
Guervyl opened this issue May 9, 2024 · 5 comments

Comments

@Guervyl
Copy link

Guervyl commented May 9, 2024

Environment details

  • OS: Centos 8
  • PHP version: 8.1
  • Package name and version: "google/apiclient": "2.16"

Steps to reproduce

  1. Generate the auth link:
            $client->setAccessType('offline');
            $client->addScope(\Google\Service\Calendar::CALENDAR_EVENTS);
            $client->addScope(\Google\Service\Calendar::CALENDAR);
            $client->setRedirectUri($redirectUri);
            $client->setIncludeGrantedScopes(true);

            $client->createAuthUrl();
  1. Fetch the token:
            $client->setAccessType('offline');
            $tokens = $client->fetchAccessTokenWithAuthCode($code);

This is the generated link https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?response_type=code&access_type=offline&client_id=

But, In the json there is no refresh_token. It only returns access_token. And the access token expires 1 hour later and I have to go and repeat the auth process again. Wich is not the case of my app. Because the token is being used in API.

Thanks!

@Lucas-Mondini
Copy link

I have the same problem

@Guervyl
Copy link
Author

Guervyl commented May 16, 2024

No one?

@mdespeuilles
Copy link

mdespeuilles commented May 16, 2024

I had the same problem. You have to put :

$client->setAccessType('offline');

Before generating the authorisation url

$client = new Client();
$client->setClientId($this->parameterBag->get('app.google_client_id'));
$client->setClientSecret($this->parameterBag->get('app.google_client_secret'));
$client->addScope("https://mail.google.com");
$client->setRedirectUri("https://localhost/api/gmail/connect");
$client->setAccessType('offline');
$service = new Gmail($client);

    if ($request->query->get('code')) {
        $token = $client->fetchAccessTokenWithAuthCode($request->query->get('code'));
        $client->setAccessToken($token);
        ........
    }

    $authUrl = $client->createAuthUrl();

    return $this->redirect($authUrl);

@aasa
Copy link

aasa commented May 16, 2024

I have the same problem and I also did put the setAccessType before createAuthUrl() but still do not work.

I have 2 functions, one to generate the Auth Url

public function createAuthUrl()
{
        $client = new Client();
        $client->setAuthConfig(ADMIN . '/config/google_api_credentials.json');
        $client->addScope('https://www.googleapis.com/auth/adwords');
        $client->setAccessType("offline");
        $client->setApprovalPrompt('force');
        $client->setRedirectUri($this->admin_site_url . $this->app['url_generator']->generate('google.oauth2.authorization-callback'));
        return $client->createAuthUrl();
}

And another as callback:

public function getTokensFromAuthCode($auth_code)
    {
        $client = new Client();
        $client->setAuthConfig(ADMIN . '/config/google_api_credentials.json');

        $client->addScope('https://www.googleapis.com/auth/adwords');
        $client->setAccessType("offline");
        $client->setApprovalPrompt('force');
        $client->setRedirectUri($this->admin_site_url . $this->app['url_generator']->generate('google.oauth2.authorization-callback'));
        $tokens = $client->fetchAccessTokenWithAuthCode($auth_code);
        $client->setAccessToken($tokens);
       
        return $tokens;
    }

@aasa
Copy link

aasa commented May 16, 2024

Ok, i was able to get the refresh token, by some reason, google only sends you the refresh token for the first time you consent/allow the permission.

So you already give the consent/allow you need to revoke the access token, you can do that by going to your google account and removing the permission or with the access token you can revoke that:

$client->revokeToken($client->getAccessToken());

The time you request the token the google will send you the refresh token.

There is also some claims that this behaviour only happens with some language libs

Check More:
https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants