Get Google refresh token when using Firebase Authentication

Viewed 132

I am developing an Android app that has a Google Sign-in, provided by Firebase Authentication. I succeeded in getting an access token but this one expires and I need to prompt the user to re-sign-in. How can I get a refresh token? I searched all over StackOverflow but I have not found a solution that works for me.

This is how I get the access token:

    val repoScope = CoroutineScope(Job() + Dispatchers.IO)
    val token = repoScope.async {
        GoogleAuthUtil.getToken(
            context,
            account,
            "oauth2:https://www.googleapis.com/auth/calendar"
        )
    }

    token.await()

I tried to get a refresh token making requests to the Google Oauth server but I always get 401 as a response

val client = OkHttpClient()
    val requestBody: RequestBody? = FormEncodingBuilder()
        .add("grant_type", "authorization_code")
        .add("client_id", CLIENT_ID)
        .add("client_secret", CLIENT_SECRET)
        .add("code", acct.serverAuthCode)
        .add("id_token", acct.idToken) 
        .build()

    val request: Request = Request.Builder()
        .url("https://www.googleapis.com/oauth2/v4/token")
        .post(requestBody)
        .build()
0 Answers
Related