android - 获取 Google API 访问权限并避免 UserRecoverableAuthIOException
问题描述
我正在使用 GMail API。
目前,我请求GET_ACCOUNTS
许可,之后,我尝试发送某种“测试”电子邮件。
如果这是用户的第一次,很可能UserRecoverableAuthIOException
会引发异常,我将使用它的意图来请求他的批准。
是否可以在尝试发送并避免出现异常之前检查这一点?
像'check_permission'这样的东西?
解决方案
你可以参考这篇文章UserRecoverableAuthIOException
:Android Drive API: getting Sys.err UserRecoverableAuthIOException if i merge code to other projects
您应该在 catch 块中捕获 and,通过调用从异常中
UserRecoverableAuthException
恢复. 启动该意图以将用户带到您的应用程序的权限页面。Intent
UserRecoverableAuthException#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); }
推荐阅读
- database - Wordpress 从 https 重定向到 http
- javascript - 我怎样才能只显示一个帖子的 100 个字符?
- reactjs - 对未调用 Blur 做出反应
- python - 两个相同的程序有不同的结果。Python
- python-3.x - Django Admin - 添加模型 B 的记录时包含模型 A 的字段
- excel - 预期:行号或标签或语句或语句结尾
- ios - 目标 C 方法不等待 Swift 方法的完成处理程序
- ios - 如何在没有 Mac 的情况下启用应用内购买
- locust - 如何在 locust 中为不同的用户分配不同的行为
- ios - Swift SceneKit - 我试图弄清楚 Node 对象是否离开屏幕