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

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

问题描述

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

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.

我正在尝试访问 AppFolder,而这只是像这样使用 GoogleApiClient:

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();

然后 fileResult.getStatus() 给我错误.

Then fileResult.getStatus() gives me the erros.

这曾经对我有用.不同的是,我手动清空了 Google Drive 上的应用数据(删除隐藏的应用数据).但我没有断开应用程序的连接 - 所以我假设 appFolder 将继续以相同的方式工作......

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.

推荐答案

更新:此问题 #4483 已于 2017 年 1 月修复.以下修复可能不再适用.

由于这仍然是一个悬而未决的问题,我已采取措施建立一种解决方法,该解决方法可以使用代码完成,而无需求助于用户干预.

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.

正如@seanpj 所说,此问题并不总是发生,并且似乎取决于 Drive 服务的同步状态.但是,如果问题确实发生了,我可以通过以下方法来规避问题.我把它贴在这里以防它可能对其他人有帮助.Javadoc 有更多信息.

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天全站免登陆