Код: Выделить всё
// Returns Gmail AuthorizationRequest to pass into Identity.authorize()
fun getGmailAuthorizationRequest(): AuthorizationRequest {
val requestedScopes = listOf(Scope(GmailScopes.GMAIL_READONLY))
return AuthorizationRequest.builder()
.setRequestedScopes(requestedScopes)
.build()
}
suspend fun revokeGmailAccessToken(): Boolean {
val token = currentGmailAccessToken ?: return false
return try {
val url = URL("https://oauth2.googleapis.com/revoke?token=$token")
withContext(Dispatchers.IO) {
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
conn.doOutput = true
conn.outputStream.flush()
conn.inputStream.bufferedReader().use { it.readText() }
conn.responseCode == 200
}
} catch (e: Exception) {
println(tag + "Error revoking token: ${e.message}")
false
}
}
// Handle result from Gmail scope request (authorization result)
fun handleAuthorizationResult(authResult: AuthorizationResult?): Boolean {
if (authResult == null) {
println(tag + "Authorization result is null.")
return false
}
val requestedScopes = listOf(Scope(GmailScopes.GMAIL_READONLY)).map { it.scopeUri }
val grantedScopes = authResult.grantedScopes
val hasGmailAccess = grantedScopes.containsAll(requestedScopes)
val accessToken = authResult.accessToken
println(tag + "Granted scopes: $grantedScopes")
println(tag + "Gmail Access: $hasGmailAccess")
println(tag + "Access token: $accessToken")
return if (hasGmailAccess && accessToken != null) {
currentGmailAccessToken = accessToken
println(tag + "Gmail Authorization Successful!")
true
} else {
currentGmailAccessToken = null
println(tag + "Gmail Authorization Denied.")
false
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... showing-up