在访问 Google Drive API 时,我在使用 GoogleAuthUtil 时遇到了一个奇怪的问题。
以下是我的代码,
@Override
protected String doInBackground(String... params){
String accountName = params[0];
//String scopes = "oauth2:profile email"; // This can work !
String scopes = "oauth2:server:client_id:7666010xxxxx-dm0d37oxxxxxxxih8k5sm5g7tajetubk.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/drive";
String token = null;
try{
token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
}
catch (IOException e){
Log.e(excpTAG, "IO Exception: " + e.getMessage());
}
catch (UserRecoverableAuthException e){
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
}
catch (GoogleAuthException e)
{
Log.e(excpTAG, "GoogleAuthException: " + e.getMessage());
}
return token;
}
在我的实证测试中,我可以通过将范围设置为来获取用户的 Google 个人资料/电子邮件的访问 token
String scopes = "oauth2:profile email";
但是,当我想访问用户的 Google Drive 时,我按照开发文档中的说明进行操作
https://developers.google.com/accounts/docs/CrossClientAuth
并将范围设置为
String scopes = "oauth2:server:client_id:766601xxxxxx-dm0dxxxxxxxxxxxxxxxx7tajetubk.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/drive";
应用执行时,Android-Studio中的Log猫总是提示发生了异常事件GoogleAuthException,事件消息为'Unknown'。
在 Android 中获取 Google 云端硬盘访问 token 的正确方法是什么?非常感谢。
最佳答案
终于找到答案了!
这个问题的主要原因应该是Google API Console和之间的PACKAGE NAME
和CERTIFICATE FINGERPRINT (SHA1)
不一致>安卓工作室
不一致会导致授权异常错误,在一定范围内。
当 Android-Studio 中的 PACKAGE NAME
和 CERTIFICATE FINGERPRINT (SHA1)
与 Google API Console 中的不一致时,Google API 不会简单地拒绝所有请求。 DriveScopes.Drive
等一些作用域触发了异常错误,而 oauth2:profile email
等其他作用域则没有。
在这种情况下,异常错误还会返回 Unknown
消息,误导我们修复某些范围引起的问题。
关于android - 在 Android 中通过 GoogleAuthUtil 从 Google Drive API 获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555315/