'Refresh token with expo auth sessions (Google)

I use for my Android app expo-auth-sessions to authorize the user via Google:

  const [request, response, promptAssync] = Google.useAuthRequest({
    androidClientId: ANDROID_CLIENT_ID,
    iosClientId: IOS_CLIENT_ID,
    expoClientId: EXPO_CLIENT_ID,
    scopes: [
      "https://www.googleapis.com/auth/drive",
    ],
  })

The request works just fine and it returns an access token, but it does not contain a refresh token.

Is there any possibility to configure the request that it also returns a refresh token or something similar to not force the user to sign in again after the access token expires?

Current response:

{
    authentication: {
      accessToken: TOKEN,
      expiresIn: "3599",
      idToken: undefined,
      issuedAt: 1644693943,
      refreshToken: undefined,
      scope:
        "https://www.googleapis.com/auth/drive",
      tokenType: "Bearer",
    },
    params: {
      access_token: TOKEN,
      expires_in: "3599",
      scope:
        "https://www.googleapis.com/auth/drive",
      token_type: "Bearer",
    },
    type: "success"
  }

Thanks in advance!



Solution 1:[1]

You'll have to add access_type: "offline" (Google APIs auth) in your auth URL because expo doesn't do that by default. For this, you can make use of the extraParams attribute in useAuthSession. However, access_type: "offline" is not supported for responseType: "token" (the default value) since the concept of refresh_token doesn't exist for implicit_grant_flow. You'll have to change that to responseType: "code" and later exchange the obtained code for access and refresh token on the server side. Overall you'll have to make the following changes -

const [request, response, promptAssync] = Google.useAuthRequest({
  androidClientId: ANDROID_CLIENT_ID,
  iosClientId: IOS_CLIENT_ID,
  expoClientId: EXPO_CLIENT_ID,
  scopes: [
    "https://www.googleapis.com/auth/drive",
  ],
  responseType: "code",
  shouldAutoExchangeCode: false,
  extraParams: {
    access_type: "offline"
  },
})

However, if you've already authorized the app once before, you might have to add prompt: 'consent' too. You can take a look at this answer for more clarity - https://github.com/googleapis/google-api-python-client/issues/213#issuecomment-205886341

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rahul Kukreja