Auth0 Home Blog Docs

How to get the Google OAuth2 refresh token?

I’m using the AuthenticationApiClient C# lib to craft a Lock login for a Google account:

var client = new AuthenticationApiClient(new Uri($"https://{_configuration["Authentication:Auth0:Domain"]}"));
            var urlb = client.BuildAuthorizationUrl();
            var uri = urlb.WithClient(_configuration["Authentication:Auth0:ClientId"])
                .WithRedirectUrl(GetBaseUri() + "Organization/AuthorizeGAFinish?id=" + id + "&returnAction=" + returnAction)
                .WithResponseType(new AuthorizationResponseType[] { AuthorizationResponseType.IdToken, AuthorizationResponseType.Code, AuthorizationResponseType.Token })
                .WithScope($"openid {Uri.EscapeUriString("")}")

Later on I get the access token with:
var mgmtToken = await GenerateManagementApiToken();
var client = new ManagementApiClient(mgmtToken, new Uri(_configuration[“Authentication:Auth0:Audience”]));

            var auth0User = await client.Users.GetAsync(userId);
            return auth0User.Identities[0].AccessToken;

However I need the refresh token as I need to do this offline. However Identities[0].RefreshToken is always null.

I have tried forcing Lock to provide access_type=offline to Google by modifying the Auth0 hosted page with:
var authParams = config.internalOptions;
authParams.access_type = ‘offline’;
authParams.approval_prompt = ‘force’;

var lock = new Auth0Lock(config.clientID, config.auth0Domain, {
auth: {
redirectUrl: config.callbackURL,
responseType: (config.internalOptions || {}).response_type ||
(config.callbackOnLocationHash ? ‘token’ : ‘code’),
params: authParams
//params: config.internalOptions

Still doesn’t work. How do I get Auth0 to store the refresh token?

:wave: @strich we could you use a Rule to store the Google Refresh Token, as explained here:

Google will only send you the refresh_token once, so if you haven’t stored it you may need to ask for it again as you mentioned using approval_prompt=force to explicitly consent again.

In addition, we can check to make sure we have added out developer keys for Google when setting up the connection un our Dashboard > Connections > Social

Hi @kimcodes,

I can try that. But it is in direct contradiction with your documentation here:

IdP Refresh Tokens can be obtained the same way as Access Tokens, using the /api/v2/user/{user-id} endpoint. The Refresh Token will be available in the identities array, under the element for the particular connection.

Is this wrong, outdated?

@kimcodes I tried setting up the rule (From the template) and I can see no additional metadata being created. It seems to be that Lock is not correctly passing the offline_access param to Google. Google is never showing ‘offline access’ as a permission type.

calling that endpoint should indeed work. We had a similar issue with the Google refresh token here where the parameters were being passed successfully to Auth0, but not to Google. That thread may be helpful for you if you wanted to give it a glance. In that situation they needed to include connection_scope as well as scope for it to work. I am going to try reproducing the issue with your code. I will need some time to test this out.