【问题标题】:Google Drive API Android account selector always returns RESULT_CANCELED for some accountsGoogle Drive API Android 帐户选择器始终为某些帐户返回 RESULT_CANCELED
【发布时间】:2016-08-10 04:56:17
【问题描述】:

我正在将 Google Drive API 集成到我的 Android 应用中,以便在 App Storage 文件夹中存储一个 JSON 文件。

为此,我在 MainActivity 的一个片段中实现了 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 返回 resultCode RESULT_OK。

如果我使用其他人的帐户,帐户选择器就会消失,并且我遇到了一个错误案例。如果我调试,我发现 resultCode 是实际的 RESULT_CANCELED。

我看不出有什么区别。看起来我的代码很标准。

有什么想法吗?

【问题讨论】:

  • 您使用的是 google 帐户(即 gmail 或 google apps 域)吗?如果您使用的是 Google Apps 域,您能否验证是否为该用户启用了云端硬盘?
  • 验证是指确保他们已经在使用 Google 云端硬盘?在我测试的两种失败的情况下都是如此。

标签: android google-api-client google-drive-android-api


【解决方案1】:

这可能是应用签名问题 - 如果您在调试时测试成功,但在发布时失败,您可能没有在 Google API 控制台中设置正确的密钥库签名 - 如果是这种情况,您应该能够将第二个签名也添加到控制台,调试和发布版本都可以正常工作。

【讨论】:

  • 原来我的 Oath2 调试凭据在从不同的 PC 构建时显然不起作用。当我尝试发布 Oauth2 凭据时,它运行良好。
猜你喜欢
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
相关资源
最近更新 更多