我有一个Android应用,正在尝试重新实现其对Google云端硬盘的访问权限。我遵循的方法是Google的REST API更新here迁移指南。我已经正确设置了我的同意页面,并且所有内容都可以在iOS上正常运行,因此,我至少认为凭据设置正确。
我从开始:
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);
}
}
如果未登录,我称其为(成功)启动身份验证流程:
@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);
}
}
在“活动”中,我使用以下方法处理结果:
@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));
}
}
...
}
哪个调用:
void handleSignInResult(Context context, Intent result) {
GoogleSignIn.getSignedInAccountFromIntent(result)
.addOnSuccessListener(account -> {
Log.d(TAG, "handleSignInResult(): " + account.getEmail());
setupClientFromAccount(context, account);
})
...
}
最后,一切都归结为:
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();
}
问题是,当我在设备上进行调试时,此方法工作正常。但是,一旦我部署到Google Play,它就会失败。它经过授权过程,并且日志记录显示它到达setupClientFromAccount()
中的handleSignInResult()
。但是任何云端硬盘操作都会失败。
现在,在我看来,这可能是(重新)签名问题,因为这是开发/调试版本与发行版/ Google Play版本之间的唯一真正区别。但是,实际上是针对发布密钥的SHA-1生成了Google Play开发者控制台上的凭据。
我缺少明显的东西吗?
(编辑:为解决此here而采取的进一步的措施,最终以失败告终。)