OAuth with Google on Android

Are you looking for help?

Yes.

Backendless Version (3.x / 6.x, Online / Managed / Pro )

6.3.4

Client SDK (REST / Android / Objective-C / Swift / JS )

Android

Application ID

4F2407C6-7893-0760-FF9F-FE16D29D3400

Expected Behavior

Expected behavior is to successfully authenticate with Google and save user info on backendless by passing a TOKEN ID.

I have one question. I’ve been trying to authenticate with Backendless using my Google account, from an android app. In my android app, I’m using One-Tap Sign in for Android, which allows me to authenticate all users with their Google account. After a successful authentication, I receive a JWT ID Token, which I’m including in: ‘Backendless.UserService.loginWithOAuth2()’ function in order to sign in to backendless and save user information.

However I’m constantly receiving an error message:
“error”: {
“code”: 401,
“message”: "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
“status”: “UNAUTHENTICATED”
}

Also I’ve created a project on my Google Cloud Platform, enabled Google + API, created OAuth credentials and added it’s secret key and client id to my backendless Google provider section as well. And copied back that redirect URL from backendless to my Google cloud platform project Oauth credentials.

Actual Behavior

I’m receiving error that authentication credential is missing.
“error”: {
“code”: 401,
“message”: "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
“status”: “UNAUTHENTICATED”
}

hello @Stefan_Jovanovic

Welcome to our community and thank you for trying out Backendless.

You should make sure that you have exchanged auth code to access token. Please checkout the following example Android-SDK/LoginWithGooglePlusSDKActivity.java at master · Backendless/Android-SDK · GitHub

I thought that ID Token can also be exchanged with Backendless, because it’s a JWT which can be decoded to get the user information. One-Tap for Android is a new API, and I cannot receive anything other then ID Token from it. I mean I can extract user info on the client side, but I thought I could do that directly with backendless. Overview of One Tap sign-in on Android  |  One Tap for Android  |  Google Developers

@Stefan_Jovanovic, unfortunately, we do not support one-tap sign-in, alternatively, you can use oAuth login check out the following topic Google Login on Android - #6 by Andriy_Konoz

@sergey.kuk Thank you for providing me with the code. However I’ve tried obtaining an Access Token out of Auth Code, but that function which I’ve copied from that code does not work and it’s returning null whenever I try to get an Access Token.

private fun getAccessToken(authCode: String?): String? {
    val tokenResponse: GoogleTokenResponse = try {
        GoogleAuthorizationCodeTokenRequest(
            NetHttpTransport(),
            GsonFactory(),
            "https://www.googleapis.com/oauth2/v4/token",
            CLIENT_ID,
            CLIENT_SECRET,
            authCode,
            "https://gallantyear.backendless.app/api/users/oauth/googleplus/authorize"
        ).execute()
    } catch (e: java.lang.Exception) {
        Log.e("AccessToken", e.message.toString())
        return null
    }
    return tokenResponse.accessToken
}

On the other side I’ve succeeded in obtaining the Auth Code.
I know that there’s another way of bypassing this with Web View in Android (I’ve read your documentation). However I think that’s a bad approach, mostly because of a user experience.

@Stefan_Jovanovic

As I see you are returning null in catch block, so probably you have got some exception

There is no any message specifically, just ‘null’. It was strange to me as well.

do you mean that there is no exception or the result is null?
try to rethrow exception instead return null

I encountered some issue, needed to uninstall the app…
The error was for running that function on a Main thread. Just fixed it.
Thank you for your help, really appreciate it! :slight_smile: