Drive API中AppFolder的父文件夹错误无效 [英] Invalid parent folder error for AppFolder in Drive API

查看:143
本文介绍了Drive API中AppFolder的父文件夹错误无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个无效的父文件夹错误,并且我已经看到了使用资源ID而不是Drive ID的解决方案,但这是另一种情况。



我'试图访问AppFolder,这只是像这样使用GoogleApiClient:

  this.appFolder = Drive.DriveApi.getAppFolder (mGoogleApiClient); 

当我稍后尝试在其中创建文件时,出现上述错误。

();

  DriveFolder.DriveFileResult fileResult = appFolder.createFile(mGoogleApiClient,changeSet,driveContentsResult.getDriveContents())。await(); 

然后fileResult.getStatus()为我提供了错误信息。



以前用于我的工作。
与我不同的是,我手动清空了Google云端硬盘中的应用数据(删除隐藏的应用数据)。
但是我没有断开应用程序 - 所以我会假设appFolder将继续以同样的方式工作...



到目前为止唯一的解决方法是卸载应用程序,但这种方式我失去了我的数据。



这是可重现的。请帮助解决方案。

解决方案

更新:此问题#4483于2017年1月修复。再次申请。



由于这仍是一个悬而未决的问题,我已采取步骤建立解决方法,不需要用户干预即可完成代码。

正如@seanpj所说,这个问题并不总是会发生,而且似乎取决于Drive服务的同步状态。但是,如果问题确实发生,下面的方法可以解决这个问题。我将它张贴在这里以防别人可能会有所帮助。 Javadoc有更多信息。

  / ** 
*适用于Android版的Drive API(GDAA)问题在隐藏的应用数据通过在线Drive界面
*(设置 - >管理应用 - >删除隐藏的应用数据)被删除后,appDataFolder变为
* appDataFolder中的b $ b *空文件。该文件在创建后立即被删除,但以正常工作顺序保留
* appDataFolder。
*请参阅< a href =https://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=4483
*目标=_ blank> apps-api-issues#4483< / a>
*< p>
*在调用Drive API失败后调用此方法,并在处理appDataFolder时返回错误代码1502
*(DriveStatusCodes.DRIVE_RESOURCE_NOT_AVAILABLE),然后再次尝试失败的操作。
*< p>
*此方法假定在调用之前已授予所有适当的权限并授权
*。
*
* @param context - 上下文
* @param account已授权的帐户名称,例如someone@gmail.com。
* @throws IOException来自REST API。
* /
private void fixAppDataFolder(Context context,String account)throws IOException {
final String [] SCOPES = {DriveScopes.DRIVE_APPDATA};
GoogleAccountCredential凭证= GoogleAccountCredential.usingOAuth2(
上下文,Arrays.asList(SCOPES))。setBackOff(new ExponentialBackOff());
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
com.google.api.services.drive.Drive driveService;

credential.setSelectedAccountName(account);
driveService = new com.google.api.services.drive.Drive.Builder(
transport,jsonFactory,凭证)
.setApplicationName(您的应用程序名称在这里)
。建立();

文件fileMetadata = new File();
fileMetadata.setName(fixAppDataFolder)
.setMimeType(text / plain)
.setParents(Collections.singletonList(appDataFolder));

文件appDataFile = driveService.files()
.create(fileMetadata)
.setFields(id)
.execute();

driveService.files()。delete(appDataFile.getId())。execute();
} // fixAppDataFolder


I get an invalid parent folder error, and I've seen the solutions to use resource ID rather than Drive ID, but it's a different scenario here.

I'm trying to access the AppFolder, and this just uses the GoogleApiClient like so:

this.appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient);

When I later try to create a file in it, I get the above error.

DriveFolder.DriveFileResult fileResult = appFolder.createFile(mGoogleApiClient, changeSet, driveContentsResult.getDriveContents()).await();

Then fileResult.getStatus() gives me the erros.

This used to work for me before. What's different is that I've manually emptied my app's data on Google Drive (delete hidden app data). But I haven't disconnected the app - so I would assume that appFolder will continue to work the same way...

So far the only workaround is uninstalling the app, but this way I lose my data.

This is reproducible. Please help.

解决方案

Update: This issue, #4483, was fixed in January 2017. The following fix may not apply anymore.

Since this continues to be an outstanding issue, I have taken steps to establish a work-around that can be done with code without resorting to user intervention.

As @seanpj says, this issue does not always occur and seems to be dependent upon the sync status of the Drive service. However, if the problem does occur, the following method works for me to circumvent the problem. I post it here in case it may be helpful to someone else. The Javadoc has more information.

/**
 * Works around the Drive API for Android (GDAA) issue where the appDataFolder becomes
 * unavailable after hidden app data is deleted through the online Drive interface
 * (Settings->Manage Apps->Delete hidden app data) by using the REST v3 API to create an
 * empty file in appDataFolder. The file is immediately deleted after creation but leaves
 * the appDataFolder in working order.
 * See <a href="https://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=4483"
 * target="_blank">apps-api-issues #4483</a>
 * <p>
 * Call this method after a call to the Drive API fails with error code 1502
 * (DriveStatusCodes.DRIVE_RESOURCE_NOT_AVAILABLE) when dealing with the appDataFolder then
 * try the failed operation again.
 * <p>
 * This method assumes that all appropriate permissions have been granted and authorizations
 * made prior to invocation.
 *
 * @param context - Context
 * @param account The account name that has been authorized, e.g., someone@gmail.com.
 * @throws IOException From the REST API.
 */
private void fixAppDataFolder(Context context, String account) throws IOException {
    final String[] SCOPES = {DriveScopes.DRIVE_APPDATA};
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
            context, Arrays.asList(SCOPES)).setBackOff(new ExponentialBackOff());
    HttpTransport transport = AndroidHttp.newCompatibleTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    com.google.api.services.drive.Drive driveService;

    credential.setSelectedAccountName(account);
    driveService = new com.google.api.services.drive.Drive.Builder(
            transport, jsonFactory, credential)
            .setApplicationName("Your app name here")
            .build();

    File fileMetadata = new File();
    fileMetadata.setName("fixAppDataFolder")
            .setMimeType("text/plain")
            .setParents(Collections.singletonList("appDataFolder"));

    File appDataFile = driveService.files()
            .create(fileMetadata)
            .setFields("id")
            .execute();

    driveService.files().delete(appDataFile.getId()).execute();
} // fixAppDataFolder

这篇关于Drive API中AppFolder的父文件夹错误无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆