Я разрабатываю приложение в Котлине и хочу использовать Google Drive для загрузки файлов. Первоначально, Google Authentication работает, но когда я прошу авторизацию для Drive, я его не понимаю. Не получайте AccessToken и не разрешены. Есть идеи? < /P>
class GoogleAuthHelper(private val context: Context) {
private val credentialManager = CredentialManager.create(context)
suspend fun signIn(activity: Activity): Pair? = withContext(Dispatchers.Main) {
try {
val googleIdOption = GetGoogleIdOption.Builder()
.setServerClientId(BuildConfig.WEB_GOOGLE_CLIENT_ID)
.setFilterByAuthorizedAccounts(false)
.setAutoSelectEnabled(true)
.setNonce(UUID.randomUUID().toString())
.build()
val request = GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.setPreferImmediatelyAvailableCredentials(false)
.build()
val response = credentialManager.getCredential(
request = request,
context = activity
)
processResponse(response, activity)
} catch (e: GetCredentialException) {
Log.e("AUTH_ERROR", "Error: ${e.type} - ${e.message}")
null
}
}
private fun processResponse(response: GetCredentialResponse, activity: Activity): Pair? {
return try {
val credential = response.credential
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
val googleIdTokenCredential = GoogleIdTokenCredential
.createFrom(credential.data)
val driveScope = Scope("https://www.googleapis.com/auth/drive.file")
val authorizationRequest = AuthorizationRequest.Builder()
.setRequestedScopes(listOf(driveScope))
.build()
val authorizationClient = Identity.getAuthorizationClient(activity)
authorizationClient.authorize(authorizationRequest)
.addOnSuccessListener { authorizationResult ->
// Log in but grantedScopes is empty
Log.e("AUTH", "TENGO AUTORIZACION OK")
Log.d("AUTH", "Authorization successful. Granted scopes: ${authorizationResult.grantedScopes}")
val token = authorizationResult.accessToken
if (token != null) {
val driveHelper = GoogleDriveHelper(context)
driveHelper.subirArchivoADrive(token)
}
}
.addOnFailureListener { exception ->
Log.e("AUTH", "Error: ${exception.message}")
}
return Pair(googleIdTokenCredential.id, googleIdTokenCredential.idToken)
}
throw Exception("Credential type is not valid")
} catch (e: Exception) {
Log.e("AUTH_ERROR", "Error al procesar la respuesta: ${e.message}")
null
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... uthorizing