Webex Messaging
Authorization
The Webex Mobile Android SDK must be initialized and authorized to interact with the Webex platform. Your application should authenticate users through an OAuth grant flow for existing Cisco Webex users or a JSON web token (JWT) for guest users without a Cisco Webex account.
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 a Acess Token
anchorTokenAuthenticator
utilizes a access token for user authentication. Create a new Webex
instance using access token authentication
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
}
})