GBG Developers

Guides   |   Obtaining a GBG Access Token using the Android SDK

Obtaining a GBG Access Token using the Android SDK

The identity provider access token is used to obtain authorization for using the GBG Identity Solution. The token is swapped for a GBG access token. Optionally, the GBG access token can be retrieved using HTTP requests, however this guide uses the SDK to perform this step.

The first step is to create an instance of the authorization service; the configuration parameter can be hard-coded to the URL shown here or a read-only value could be used to save re-typing the URL when creating other services with the SDK:

// create an instance of the AuthService
val authService  = AuthService(Config("https:api.gbgplc.com/"))

Next, use the identity provider access token to create an AuthRequest object. With OAuth, some of these fields are to be hard-coded as can be seen in the code snippet below. The identity provider access token is populated into the idToken parameter.

// Create an auth request using the OAuth2 token
val authRequest = AuthRequest(
    grantType = "id_auth", // hard-coded; leave this as the text “id_auth”
    clientId = "ro.client", // hard-coded; leave this as the text “ro.client”
    scope = "",
    clientSecret = "secret", // hard-coded; leave this as the text “secret”
    idToken = accessToken
)

The SDK is used to send a request to the GBG Identity Solution. The returned GBG access token can then be placed into the TokenStore object as shown here:

//Pass the request into auth service and handle the result
authService.authenticate(authRequest, resultListener = object : VerifyResultListener<Response<AuthResponse>> {
    override fun success(response: Response<AuthResponse>) {
        val accessToken = response.body()?.accessToken ?: ""
        TokenStore.storeAccessToken(accessToken)
        Log.d(TAG, "success: obtained GBG access token")
        Log.d(TAG, accessToken)
    }

    override fun failure(error: ErrorResponse) {
        Log.e(TAG, "error when attempting to obtain GBG access token")
        Log.e(TAG, error.message)
    }
})

The code snippet above displays some text when debugging the application. To test the procedure, run the application and the run output should indicate success. If there is an error response, refer to Debugging with the Profiler in order to see examples of how to troubleshoot this.