gpt4 book ai didi

android - 如何确定应用程序是否已与 Google Drive 断开连接?

转载 作者:行者123 更新时间:2023-12-04 23:45:19 26 4
gpt4 key购买 nike

我正在构建一个应用程序,我将应用程序数据存储在 Google Drive 上的应用程序特定文件夹中。我已经能够设置与文件存储和检索相关的所有内容。我面临的问题是关于权限。用户可以选择断开应用程序与 Google Drive 设置面板的连接。
我使用 DriveScopes.DRIVE_APPDATA含义 https://www.googleapis.com/auth/drive.appdata保存数据的范围。
enter image description here
我试图弄清楚如何确定这是否发生在应用程序端。如果我尝试继续使用与驱动器相关的 api,并且应用程序被断开,那么它会崩溃并显示 UserRecoverableAuthException .

com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:297)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:476)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:409)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:526)
at abhiank.maplocs.ui.drivesync.DriveSyncService.onHandleIntent(DriveSyncService.kt:68)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:78)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
at com.google.android.gms.auth.zze.zzb(Unknown Source:13)
at com.google.android.gms.auth.zzd.zza(Unknown Source:77)
at com.google.android.gms.auth.zzd.zzb(Unknown Source:20)
at com.google.android.gms.auth.zzd.getToken(Unknown Source:7)
at com.google.android.gms.auth.zzd.getToken(Unknown Source:5)
at com.google.android.gms.auth.zzd.getToken(Unknown Source:2)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source:55)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:267)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:292)
我尝试了以下方法来确定应用程序是否没有权限或范围。
  • 看里面的数据GoogleSignInAccountGoogleSignIn.getLastSignedInAccount(this) 收到的实例.这在 account.grantedScopes() 中有以下可用范围应用程序断开连接后。 drive.appdata即使应用程序已断开连接,也会显示。
  • [https://www.googleapis.com/auth/drive.appdata, https://www.googleapis.com/auth/userinfo.profile, https://www.googleapis.com/auth/userinfo.email, openid, profile, email]
  • 我最后尝试的是hasPermissions GoogleSignIn 中可用的方法.我检查了此调用是否可以使用 APP_DATA 范围,它返回 true。所以那里也没有帮助。
  • GoogleSignIn.hasPermissions(account, Scope(DriveScopes.DRIVE_APPDATA))
    我现在真的被困住了。任何帮助将不胜感激。谢谢。

    最佳答案

    我最终在与驱动器相关的代码周围使用了 try-catch 并捕获了 UserRecoverableAuthIOException .根据documentation , 当 -

    UserRecoverableAuthExceptions signal Google authentication errors thatcan be recovered with user action, such as a user login.


    这对我来说效果很好。考虑到这个问题在 2 年内没有收到任何其他答案,似乎没有任何方法可以通过 API 或 SDK 调用获取有关应用程序是否断开连接的信息。
    这是我使用的代码
    fun getGoogleDriveService(context: Context): Drive {
    val credential = GoogleAccountCredential.usingOAuth2(context, setOf(DriveScopes.DRIVE_APPDATA))
    credential.selectedAccount = GoogleSignIn.getLastSignedInAccount(context)!!.account
    return Drive.Builder(NetHttpTransport(), GsonFactory(), credential)
    .setApplicationName(DriveSyncService.APP_NAME)
    .build()
    }

    try {
    val driveService = getGoogleDriveService(this)
    var fileList = driveService.files().list().execute()
    //...
    //more code
    } catch (e: UserRecoverableAuthIOException) {
    /*
    Doing a sign-out on the googleSignInClient so that there is no mismatch
    in sign-in state and so that when I start sign-in process again, it
    starts afresh
    */
    googleSignInClient.signOut()
    /*
    Then I show a pop up telling user that app was disconnected and
    to sign in again. And then on click I start the sign-in flow again.
    */
    } catch (e: GoogleJsonResponseException) {
    //https://googleapis.dev/java/google-api-client/latest/index.html?com/google/api/client/googleapis/json/GoogleJsonResponseException.html
    //404 is file being updated/deleted was not found
    if (e.message != null && e.message!!.contains("storageQuotaExceeded")) {
    //todo handle storage exceeded error. Inform user
    }
    } catch (e: IOException) {
    //todo handle network error
    }

    关于android - 如何确定应用程序是否已与 Google Drive 断开连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64820763/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com