[英]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;
}
在我的經驗測試中,我可以通過將范圍設置為來獲取用戶的Google個人資料/電子郵件的訪問令牌
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";
執行該應用程序時,Android-Studio中的Log cat始終提到發生了異常事件GoogleAuthException,並且事件消息為“未知”。
在Android中獲取Google雲端硬盤訪問令牌的正確方法是什么? 非常感謝。
終於我找到了答案!
造成此問題的主要原因應該是Google API控制台和Android-Studio之間的“ PACKAGE NAME
和“ CERTIFICATE FINGERPRINT (SHA1)
不一致
在某些情況下,不一致會導致授權異常錯誤。
如果Android-Studio中的“ PACKAGE NAME
和“ CERTIFICATE FINGERPRINT (SHA1)
與Google API控制台中的“ PACKAGE NAME
和“ CERTIFICATE FINGERPRINT (SHA1)
不一致,則Google API不會簡單地拒絕所有請求。 某些驅動器(如DriveScopes.Drive
觸發了異常錯誤,而其他諸如oauth2:profile email
未觸發。
在這種情況下,異常錯誤還會返回“ Unknown
消息,從而使我們無法正確解決由某些范圍引起的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.