This article provides code examples explaining how to perform this authorization using either a built in Webview or an external browser.
anchorAuthorize Using a OAuth
anchorOAuthWebViewAuthenticator utilizes a WebView for user authentication. Ensure the WebView is integrated into the view hierarchy before invoking the authorize method.
Initialize a Webex instance with OAuth authentication:
val clientId: String = "YOUR_CLIENT_ID"
val clientSecret: String = "YOUR_CLIENT_SECRET"
val redirectUri: String = "https://webexdemoapp.com"
val scope: String = "spark:all"
val email = "EMAIL_ID_OF_END_USER" // Email address for the end user
val authenticator: OAuthWebViewAuthenticator = OAuthWebViewAuthenticator(clientId, clientSecret, scope, redirectUri, email)
val webex = Webex(application, authenticator)
webex.initialize(CompletionHandler { result ->
if (result.error == null) {
// Already authorized
} else {
authenticator.authorize(loginWebview, CompletionHandler { result ->
if (result.error != null) {
// Handle the error
}else{
// Authorization Successful
}
})
}
})
anchorAuthorize Using a JSON Web Token(JWT)
anchorJWTAuthenticator utilizes a JWT token for user authentication. Create a new Webex instance using JWT authentication
val token: String = "jwt_token"
val authenticator: JWTAuthenticator = JWTAuthenticator()
val webex = Webex(application, authenticator)
webex.initialize(CompletionHandler { result ->
if (result.error == null) {
// Already authorised
} else {
authenticator.authorize(token, CompletionHandler { result ->
if (result.error != null) {
// Handle the error
}else{
// Authorization Successful
webex.spaces.setOnInitialSpacesSyncCompletedListener( CompletionHandler { result->
if(result.isSuccessful){
// Initial Sync successful
}
}
}
})
}
})
anchorAuthorize Using an Access Token
anchorTokenAuthenticator utilizes a access token for user authentication. Create a new Webex instance using access token authentication
For guest users created using the new Service Apps based Guest Management, the generated guest's token should be used with Access Token-based authentication flow.
val token: String = "<your-access-token>"
val expiryInSeconds = 60 // Expiry time in seconds
val authenticator: TokenAuthenticator = TokenAuthenticator()
val webex = Webex(application, authenticator)
webex.initialize(CompletionHandler { result ->
if (result.error == null) {
// Already authorised
} else {
authenticator.authorize(token, expiryInSeconds, CompletionHandler { result ->
if (result.error != null) {
// Handle the error
}else{
// Authorization successful
webex.spaces.setOnInitialSpacesSyncCompletedListener( CompletionHandler { result->
if(result.isSuccessful){
// Initial Sync successful
}
}
}
})
}
})
anchorAuthorize Using an External Browser
anchorAs an alternative, OAuthAuthenticator can be used, which eliminates the need for a WebView. Developers have the flexibility to use any browser or WebView for authentication and then provide the authorization code to the SDK through OAuthAuthenticator:
val clientId: String = "YOUR_CLIENT_ID"
val clientSecret: String = "YOUR_CLIENT_SECRET"
val redirectUri: String = "https://webexdemoapp.com"
val scope: String = "spark:all"
val email = "EMAIL_ID_OF_END_USER" // Email address for the end user.
val authenticator: OAuthAuthenticator = OAuthAuthenticator(clientId, clientSecret, scope, redirectUri, email)
val webex = Webex(application, authenticator)
webex.initialize(CompletionHandler { result ->
if (result.error == null) {
// Already authorized
} else {
authenticator.getAuthorizationUrl(CompletionHandler { result ->
if (result.error != null) {
// Handle the error
}else{
// result.data
// Use this URL to launch in either Webview or a web browser and then pass the authCode to the Authenticator's authorize method.
}
})
}
})
authenticator.authorize(authCode, CompletionHandler { result ->
if (result.error != null) {
// Handle the error
}else{
// Authorization successful
}
})
anchorAuthentication Event Delegate
anchorExtend the WebexAuthDelegate interface to receive authentication-related event callbacks.
interface WebexAuthDelegate {
/**
* This notifies when user is logged out and re-login is required.
*/
fun onReLoginRequired() {}
}
Example usage:
class SomeClass : WebexAuthDelegate {
...
override fun onReLoginRequired() {
// Handle signout and perform any cleanup if required.
}
...
}