I got a weird problem in using GoogleAuthUtil when accessing the Google Drive API.
Following is my code,
@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;
}
In my empirical test, I can acquire the access token for user's Google profile/email by setting the scopes as
String scopes = "oauth2:profile email";
However, when I want to access user's Google Drive, I followed the instructions in development document
https://developers.google.com/accounts/docs/CrossClientAuth
and set the scopes as
String scopes = "oauth2:server:client_id:766601xxxxxx-dm0dxxxxxxxxxxxxxxxx7tajetubk.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/drive";
When the app executed, the Log cat in Android-Studio always mentioned that the exception event GoogleAuthException occured, and the event message is 'Unknown'.
What is the correct way to get the access token for Google Drive in Android? Thanks a lot.
Finally I found the answer !
The main cause of this problem should be the inconsistency of PACKAGE NAME
and CERTIFICATE FINGERPRINT (SHA1)
between Google API Console and Android-Studio
The inconsistency will cause the authorization exception errors, in certain scopes.
When PACKAGE NAME
and CERTIFICATE FINGERPRINT (SHA1)
in Android-Studio don't coincide with those in Google API Console , the Google API would not simply reject all the requests. Some scopes like DriveScopes.Drive
triggered the exception error, while others like oauth2:profile email
doesn't.
In this case, the exception error also return the Unknown
message, misdirecting us to fixing the issues aroused by certain scopes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.