【问题标题】:Google Drive API: The provided file ID is not usableGoogle Drive API:提供的文件 ID 不可用
【发布时间】:2015-08-27 02:24:45
【问题描述】:

我应该遵循哪些规则来设置文件的自定义 ID?我尝试了简短的“12345”、“abcde”、44 个字符的字母数字字符串、UUID.randomUUID().toString()(带/不带破折号)-所有尝试都返回“提供的文件 ID 不可用”。我找不到任何书面要求。代码:

Drive drive = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credentials).build();
FileContent mediaContent = new FileContent("image/png", tempFile);
File body = new File();
body.setId(...);
body.setTitle(...);
body.setMimeType("image/png");
File result = drive.files().insert(body, mediaContent).execute();

回应:

400 Bad Request
{
    "code": 400,
    "errors":
    [{
        "domain": "global",
        "location": "file.id",
        "locationType": "other",
        "message": "The provided file ID is not usable",
        "reason": "invalid"
    }],
    "message": "The provided file ID is not usable"
}

当我尝试设置 ID 时,相同的代码会正确地将我的文件上传到云端硬盘。

【问题讨论】:

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


    【解决方案1】:

    您可以设置 ID,但您必须使用 Google 提供给您的预生成 ID。 我遇到了同样的问题,所以我深入研究了 javadoc,并偶然发现了 GeneratedIds 类。这是对我有用的代码:

        int numOfIds = 20;
        GeneratedIds allIds = null;
        try {
            allIds = driveService.files().generateIds()
                    .setSpace("drive").setCount(numOfIds).execute();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<String> generatedFileIds = allIds.getIds();
    

    【讨论】:

    【解决方案2】:

    您没有设置 ID,它是由 Google Drive 在创建时分配的。您所说的 ID 是您在访问 drive.google.com、右键单击对象(文件夹/文件)并选择“获取链接”时看到的字符串。你会得到类似的东西:

    https://drive.google.com/open?id=0B1blahblahblahblahfdR25i

    字符串 '0B1blahblahblahblahfdR25i' 是 ID。

    测试一下。从您的 drive.google.com 获取它,转到 bottom this page - TryIt!,将 0B1blahblahblahblahfdR25i 粘贴到 fileId 字段中。

    回到你的问题。您显然正在尝试创建一个文件,试试这个:

    com.google.api.services.drive.Drive mGOOSvc:
    ...
    /**************************************************************************
       * create file in GOODrive
       * @param prnId  parent's ID, (null or "root") for root
       * @param titl  file name
       * @param mime  file mime type
       * @param file  file (with content) to create
       * @return      file id  / null on fail
       */
      static String createFile(String prnId, String titl, String mime, java.io.File file) {
        String rsId = null;
        if (mGOOSvc != null && mConnected && titl != null && mime != null && file != null) try {
          File meta = new File();
          meta.setParents(Arrays.asList(new ParentReference().setId(prnId == null ? "root" : prnId)));
          meta.setTitle(titl);
          meta.setMimeType(mime);
    
          File gFl = mGOOSvc.files().insert(meta, new FileContent(mime, file)).execute();
          if (gFl != null)
            rsId = gFl.getId();
        } catch (Exception e) { UT.le(e); }
        return rsId;  
      } 
    

    返回,rsId 是您要查找的 ID。

    它取自Android demo here(用于解释上下文),但那里的 Api 调用应该几乎相同。您实际上可以从中提取一些 CRUD 原语。

    祝你好运

    【讨论】:

    • 谢谢!但是你确定我不能设置自定义 ID 吗?那为什么 File.setId() 方法是公开的呢?我绝对可以在 Google Calendar API 中为事件设置自定义 ID,所以我想我可以在 Drive API 中做同样的事情。
    • 不确定,但我从未听说过。我一直认为这是创造的结果是理所当然的。您通过提供 METADATA 和 CONTENT 创建对象并获取它的 ID。稍后使用该 ID 对 Goo 对象(文件夹、文件)的任何引用。阅读、更新、垃圾/删除。请参阅我指出的 REST.java 中的原语。
    • Google 日历事件 ID 也必须是唯一的,但您可以设置自己的(当然要遵循记录在案的规则)。同样的问题仍然存在 - 为什么 setId() 方法存在并且是公开的。
    • 如果您检查Files resource representation id 未列为可写。
    • 实际上,如果您使用 generateIds 服务 (developers.google.com/drive/v2/reference/files/generateIds) 创建的值,您可以 设置 ID。不幸的是,您不能设置任意 ID,即使它们是 UUID。如果你尝试,你会得到“提供的文件 ID 不可用”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多