我正在将 Google Drive API 集成到我的 Android 应用中,以将 JSON 文件存储在 App Storage 文件夹中。
为此,我在 MainActivity 内的 fragment 中实现了 Google Drive API。
当我执行此代码时,它会按预期使用 SIGN_IN_REQUIRED 代码命中 onConnectionFailed 方法。我执行 startResolutionForResult 并出现帐户选择器。
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString());
if (connectionResult.hasResolution()) {
if(!mConnectionResolutionInProgress)
{
try {
mConnectionResolutionInProgress = true;
connectionResult.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
// Unable to resolve, message user appropriately
showMessage("There was an issue connecting to Google Drive services.");
}
}
else
{
mConnectionResolutionInProgress = false;
showMessage("Canceling export/import action");
}
}
else
{
mConnectionResolutionInProgress = false;
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), getActivity(), 0).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE_RESOLUTION)
{
mConnectionResolutionInProgress = false;
if(resultCode == Activity.RESULT_OK)
{
if(!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting())
{
ConnectToGoogleDrive();
}
}
}
}
private void ConnectToGoogleDrive()
{
if(mGoogleApiClient == null)
{
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
奇怪的是,我的个人帐户(和我的工作帐户)运行得很好。我单击我的帐户,然后对话框消失,并替换为权限对话框。如果我接受,那么手术就会完美地继续。 onActivityResult 返回结果代码 RESULT_OK。
如果我使用其他人的帐户,帐户选择器就会消失,并且我会遇到一个错误情况。如果我调试,我会发现 resultCode 是实际的 RESULT_CANCELED。
我不明白有什么区别。看起来我的代码非常标准。
有什么想法吗?
最佳答案
这可能是应用程序签名问题 - 如果您在调试时测试成功,但在发布时失败,则您可能没有在 Google API 控制台中设置正确的 keystore 签名 - 如果是这种情况,您应该能够将第二个签名也添加到控制台,调试和发布版本都可以正常工作。
关于android - 对于某些帐户,Google Drive API Android 帐户选择器始终返回 RESULT_CANCELED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38864543/