【发布时间】: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