GBG Developers

Guides   |   Obtain a GBG Access Token using the SDK

Obtain a GBG Access Token using the 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.

First, create a GBGAuthService object as shown below. Next, use the identity provider access token to create a GBGAuthCredentials object. Some of these fields are to be hard-coded as can be seen in the code snippet. The identity provider access token is populated using the withAccessToken method.

private lazy var authService = GBGAuthService()
// Create an auth request using the OAuth2 token
let authRequest = GBGAuthCredentials()
    .withGrantType("id_auth")
    .withClientId("ro.client")
    .withClientSecret("secret")
    .withAccessToken(accessToken)

The SDK is used to send a request to the GBG Identity Solution. The returned GBG access token is automatically stored in the iOS keychain, ready for use by any other services in the SDK.

//Pass the request into auth service and handle the result
self.authService.authenticate(authRequest, success: { result in
    print(“success: obtained GBG access token: \(response?.body)”)
}, failure: { error in
    self.showPopup(message: "error when attempting to obtain GBG access token")  
})

The code snippet above will print a success message to the console when testing the application. To test the procedure, run the application and the message should indicate success. If there is an error response, you may wish to examine the HTTP traffic during application development, to see if the request was sent to the GBG cloud service, and what the response was. To do this Charles for iOS can be used.