首页 > 解决方案 > 获取 Google API 访问权限并避免 UserRecoverableAuthIOException

问题描述

我正在使用 GMail API。
目前,我请求GET_ACCOUNTS许可,之后,我尝试发送某种“测试”电子邮件。

如果这是用户的第一次,很可能UserRecoverableAuthIOException会引发异常,我将使用它的意图来请求他的批准。

是否可以在尝试发送并避免出现异常之前检查这一点?
像'check_permission'这样的东西?

标签: androidgmailgoogle-oauthgmail-api

解决方案


你可以参考这篇文章UserRecoverableAuthIOExceptionAndroid Drive API: getting Sys.err UserRecoverableAuthIOException if i merge code to other projects

您应该在 catch 块中捕获 and,通过调用从异常中 UserRecoverableAuthException恢复. 启动该意图以将用户带到您的应用程序的权限页面。IntentUserRecoverableAuthException#getIntent()OAuth2

示例代码 -

try {
    Drive service = Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
        .build();
    // Do whatever you want with the Drive service
} catch (UserRecoverableAuthIOException e) {
    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}

推荐阅读