通过 Android 中的 GoogleAuthUtil 从 Google Drive API 获取访问令牌
Get Access Token From Google Drive API by GoogleAuthUtil in Android
我在访问 Google 驱动器 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;
}
在我的经验测试中,我可以通过将范围设置为
来获取用户Googleprofile/email的访问令牌
String scopes = "oauth2:profile email";
但是,当我想访问用户的 Google 驱动器时,我按照开发文档
中的说明进行操作
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";
app执行时,Android-Studio中的Log cat总是提示异常事件GoogleAuthException发生,事件消息为'Unknown'.
在 Android 中获取 Google 驱动器访问令牌的正确方法是什么?非常感谢。
终于找到答案了!
导致这个问题的主要原因应该是GoogleAPIConsole和PACKAGE NAME
和CERTIFICATE FINGERPRINT (SHA1)
不一致Android-工作室
在某些范围内,不一致会导致授权异常错误。
当Android-Studio中的PACKAGE NAME
和CERTIFICATE FINGERPRINT (SHA1)
与[=40中的不一致时=]APIConsole,GoogleAPI不会简单的拒绝所有的请求。 DriveScopes.Drive
等一些范围触发了异常错误,而 oauth2:profile email
等其他范围则没有。
在这种情况下,异常错误也 return Unknown
消息,误导我们修复某些范围引起的问题。
我在访问 Google 驱动器 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;
}
在我的经验测试中,我可以通过将范围设置为
来获取用户Googleprofile/email的访问令牌String scopes = "oauth2:profile email";
但是,当我想访问用户的 Google 驱动器时,我按照开发文档
中的说明进行操作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";
app执行时,Android-Studio中的Log cat总是提示异常事件GoogleAuthException发生,事件消息为'Unknown'.
在 Android 中获取 Google 驱动器访问令牌的正确方法是什么?非常感谢。
终于找到答案了!
导致这个问题的主要原因应该是GoogleAPIConsole和PACKAGE NAME
和CERTIFICATE FINGERPRINT (SHA1)
不一致Android-工作室
在某些范围内,不一致会导致授权异常错误。
当Android-Studio中的PACKAGE NAME
和CERTIFICATE FINGERPRINT (SHA1)
与[=40中的不一致时=]APIConsole,GoogleAPI不会简单的拒绝所有的请求。 DriveScopes.Drive
等一些范围触发了异常错误,而 oauth2:profile email
等其他范围则没有。
在这种情况下,异常错误也 return Unknown
消息,误导我们修复某些范围引起的问题。