如果我尝试像这样请求 DRIVE_APPDATA 范围的授权(无需事先登录):
val authorizationRequest =
AuthorizationRequest
.builder()
.setRequestedScopes(listOf(Scope(DriveScopes.DRIVE_APPDATA)))
.build()
Identity
.getAuthorizationClient(context)
.authorize(authorizationRequest)
.addOnSuccessListener { authorizationResult ->
if (authorizationResult.hasResolution()) {
// access needs to be granted by the user
val pendingIntent = authorizationResult.pendingIntent
try {
startIntentSenderForResult(pendingIntent!!.intentSender, 999, null, 0, 0, 0, null)
} catch (e: SendIntentException) {
Log.i("TTTT", "Couldn't start Authorization UI: " + e.localizedMessage)
}
} else {
// access already granted, continue with user action
Log.i("TTTT", "access already granted")
// saveToDriveAppFolder(authorizationResult)
}
}
.addOnFailureListener { e -> Log.i("TTTT", "Failed to authorize", e) }
该请求似乎工作正常:
但我有几个疑问:
这是正确的;授权不需要登录(从授权的角度来看;我想您很可能希望您的用户使用您的帐户管理系统登录到您的应用程序)。如果您调用 Identity signOut 或其等效的 CredentialManager clearCredentialState,它应该再次提示用户使用帐户选择器。