Android OAuth
OAuth is used to open a web view and get the access token to download the vehicle's Access Certificate. At first the access token
should be requested with getAccessToken() method. Subsequently the refreshAccessToken()
method can be used for token refreshing.
Getting the instance
Get the OAuth instance.
Declaration
public OAuth getOAuth();
Returns
The OAuth instance
Discussion
OAuth can be accessed from the HMKit object. A device certificate must be set before
Example
val oauth = HMKit.getInstance().getOAuth();
Methods
Get the Access Token
Declaration
fun getAccessToken(
activity: Activity,
appId,
authUrl,
clientId,
redirectScheme,
tokenUrl,
startDate: Calendar? = null,
endDate: Calendar? = null,
state? = null,
completionHandler: CompletionHandler)
Parameters
activity | The activity the web view is launched from |
appId | The application's identifier |
authUrl | The base authentication URI |
clientId | The client identifier |
redirectScheme | The redirect scheme URI used to redirect back to the app |
tokenUrl | The token url where the access token is requested from |
startDate | The start date |
endDate | The end date |
state | The optional state |
completionHandler | The completion handler that is invoked with access and refresh tokens or an error |
Discussion
Start the access token flow, where a web view is opened for the user to select a vehicle for which an Access Token is downloaded. That access token is returned to the user and can be used to download the vehicle's Access Certificate via HMKit.downloadAccessCertificate
Read More
Example
/*
HMKit initialised.
Called inside an Activity.
*/
val appId = "..."
val authUrl = "..."
val clientID = "..."
val redirectScheme = "..."
val tokenUrl = "..."
HMKit.getInstance().oAuth.getAccessToken(
this,
appId,
authUrl,
clientID,
redirectScheme,
tokenUrl
) { accessToken, errorMessage ->
if (accessToken != null) {
// Optional: store the refresh token and expiration data
this.refreshToken = accessToken.refreshToken
this.expireDate = (System.currentTimeMillis() / 1000).toInt() + accessToken.expiresIn
// Download the vehicle's Access Certificate
HMKit.getInstance().downloadAccessCertificate(accessToken.accessToken, object : HMKit.DownloadCallback {
// response handling
})
}
else {
// if there is no access token, there is an error
onError(errorMessage!!)
}
}
Refresh the Access Token
Declaration
fun refreshAccessToken(
tokenUrl,
clientId,
refreshToken,
completionHandler: CompletionHandler)
Parameters
tokenUrl | The token url where the access token is requested from |
clientId | The client identifier |
refreshToken | The Refresh Token |
completionHandler | The completion handler that is invoked with access and refresh tokens or an error |
Discussion
Refresh the previously aqcuired Access Token, without the need for user interaction in a web view. Use the refreshed Access Token to download an Access Certificate with HMKit.downloadAccessCertificate
Read More
Example
/*
HMKit is initialised.
Called inside an Activity.
*/
val tokenUrl = "..."
val clientId = "..."
val refreshToken = "..." // stored previously from the getAccessToken result
HMKit.getInstance().oAuth.refreshAccessToken(
this,
tokenUrl,
clientId,
refreshToken
) { accessToken, errorMessage ->
// same response as for the getAccessToken
if (accessToken != null) { }
else { }
}