Google Drive API Android 帐户选择器总是 returns RESULT_CANCELED 对于某些帐户
Google Drive API Android account selector always returns RESULT_CANCELED for some accounts
我正在将 Google 驱动器 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 returns resultCode RESULT_OK.
如果我使用其他人的帐户,帐户选择器就会消失,我遇到了一个错误案例。如果我调试,我会看到 resultCode 是实际的 RESULT_CANCELED.
我看不出有什么区别。看起来我的代码很标准。
有什么想法吗?
这可能是一个应用程序签名问题 - 如果您在调试时测试成功,但在发布时失败,您可能没有在 Google API 控制台中设置正确的密钥库签名 -如果是这种情况,您应该也可以将第二个签名添加到控制台,并且调试和发布版本都可以正常工作。
我正在将 Google 驱动器 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 returns resultCode RESULT_OK.
如果我使用其他人的帐户,帐户选择器就会消失,我遇到了一个错误案例。如果我调试,我会看到 resultCode 是实际的 RESULT_CANCELED.
我看不出有什么区别。看起来我的代码很标准。
有什么想法吗?
这可能是一个应用程序签名问题 - 如果您在调试时测试成功,但在发布时失败,您可能没有在 Google API 控制台中设置正确的密钥库签名 -如果是这种情况,您应该也可以将第二个签名添加到控制台,并且调试和发布版本都可以正常工作。