问题
I have an Android app that I am trying to (re)implement Google Drive access for. The method I'm following is Google's migration guide to the update REST API here. I have my consent page set up properly, and everything works on the iOS side of things, so I assume at very least the credentials are set up properly.
I start with:
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(DriveScopes.DRIVE))
.requestEmail()
.build();
mSignInClient = GoogleSignIn.getClient(activity, signInOptions);
Task<GoogleSignInAccount> task = mSignInClient.silentSignIn();
if (task.isSuccessful()) {
// There's an immediate result available
GoogleSignInAccount account = task.getResult();
if (account != null) {
setupClientFromAccount(activity, account);
}
}
If not logged in, I call this, which (successfully) launches the auth flow:
@Override
public void login(final Activity activity) {
if (isLoggedIn()) {
return;
}
Task<GoogleSignInAccount> task = mSignInClient.silentSignIn();
if (task.isSuccessful()) {
// There's an immediate result available
GoogleSignInAccount account = task.getResult();
if (account != null) {
setupClientFromAccount(activity, account);
}
}
else {
// The result of the sign-in Intent is handled in onActivityResult
activity.startActivityForResult(mSignInClient.getSignInIntent(), REQUEST_GOOGLE_SIGN_IN);
}
}
In my Activity I handle the result using:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == REQUEST_GOOGLE_SIGN_IN) {
if (resultCode == Activity.RESULT_OK && resultData != null) {
handleSignInResult(this, resultData);
} else {
Log.e(TAG, String.format("Unable to complete Google sign-in (resultCode: %d)", resultCode));
}
}
...
}
which calls:
void handleSignInResult(Context context, Intent result) {
GoogleSignIn.getSignedInAccountFromIntent(result)
.addOnSuccessListener(account -> {
Log.d(TAG, "handleSignInResult(): " + account.getEmail());
setupClientFromAccount(context, account);
})
...
}
Finally, everything comes down to:
private void setupClientFromAccount(Context context, GoogleSignInAccount account) {
// Use the authenticated account to sign in to the Drive service
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(context, Collections.singleton(DriveScopes.DRIVE));
credential.setSelectedAccount(account.getAccount());
mDrive = new Drive.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName("App Name")
.build();
}
The problem is that this works just fine when I'm debugging, on my device. Once I deploy to Google Play, however, it fails. It goes through the authorization process, and logging shows that it gets to the setupClientFromAccount()
in handleSignInResult()
. But any Drive operations fail.
Now, that sounds to me like it may be a (re)signing issue, since that's the only real difference between the development/debug version and the release/Google Play version. But the credentials on the Google Play developer console are actually generated for the release key's SHA-1.
Am I missing something obvious?
(EDIT: Some further, ultimately unsuccessful steps taken to try to resolve this here.)
来源:https://stackoverflow.com/questions/60964590/having-trouble-getting-google-drive-authorization