Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google drive api 使用文档列表API 3.0创建空文件_Google Drive Api_Google Docs Api - Fatal编程技术网

Google drive api 使用文档列表API 3.0创建空文件

Google drive api 使用文档列表API 3.0创建空文件,google-drive-api,google-docs-api,Google Drive Api,Google Docs Api,我正在开发一个功能,需要创建一个新的空文件而不是谷歌驱动器的文档,现在我使用的是文档列表API 3.0,我指的是文档:。 我会上传一个零字节的文件到谷歌硬盘来生成空文件 现在我在请求步骤1和请求步骤2期间遇到问题。在第一次Post[Resubable create media link]请求后,我成功获得了上传位置。然后,当我请求将方法放到该位置时,我得到了一个404NotFound错误。所有请求都具有GData版本:3.0和授权:accessToken头 我在论坛上搜索了很多,找到了如何创建空

我正在开发一个功能,需要创建一个新的空文件而不是谷歌驱动器的文档,现在我使用的是文档列表API 3.0,我指的是文档:。 我会上传一个零字节的文件到谷歌硬盘来生成空文件

现在我在请求步骤1和请求步骤2期间遇到问题。在第一次Post[Resubable create media link]请求后,我成功获得了上传位置。然后,当我请求将方法放到该位置时,我得到了一个404NotFound错误。所有请求都具有GData版本:3.0和授权:accessToken头

我在论坛上搜索了很多,找到了如何创建空文档的方法,但却找不到如何创建空文件。这是我的代码,谁能帮我看看哪个部分出错了?提前谢谢

private final static String PARAM_CONTENT_TYPE = "Content-Type";
private final static String PARAM_UPLOAD_LENGTH = "X-Upload-Content-Length";
private final static String PARAM_UPLOAD_TYPE = "X-Upload-Content-Type";
private final static String CONTENT_TYPE_XML = "application/atom+xml";
private final static String URI_RESUMABLE_RESOURCE = "https://docs.google.com/feeds/upload/create-session/default/private/full";
private final static String ENTITY_NEW_FILE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\" "
        + "xmlns:docs=\"http://schemas.google.com/docs/2007\"><title>{0}</title></entry>";

@Override
public boolean createNewFile() throws IOException {
    String uri = null;
    if (ROOT.equals(parentResourceId))
        uri = URI_RESUMABLE_RESOURCE;
    else
        uri = URI_RESUMABLE_RESOURCE + "/%3A" + parentResourceId + "/contents";
    try {
        StringEntity entity = new StringEntity(MessageFormat.format(ENTITY_NEW_FILE, getName()), Constants.ENCODING);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put(PARAM_CONTENT_TYPE, CONTENT_TYPE_XML);
        headers.put(PARAM_UPLOAD_LENGTH, "0");
        headers.put(PARAM_UPLOAD_TYPE, "text/plain");
        Map<String, String> params = new HashMap<String, String>();
        params.put("convert", "false");
        HttpResponse response = helper.execMethodAsResponse(uri, new PostMethod(entity), headers, params);
        String location = null;
        if ((location = response.getFirstHeader("Location").getValue()) != null) {
            headers = new HashMap<String, String>();
            headers.put(PARAM_CONTENT_TYPE, "text/plain");
            headers.put("Content-Range", "bytes 0-0/0");
            //FIXME: Problem occurs here, this put invocation will return 404 not found error.
            JsonObject obj = helper.execMethodAsJson(location, new PutMethod(new ByteArrayEntity(new byte[0])), headers, null);
            if (obj != null) {
                decorateFile(this, obj.get("entry").getAsJsonObject());
                return true;
            }
        }
    } catch (UnsupportedEncodingException e) {
    }
    return false;
}

在Google Drive上创建空文件的最简单方法是使用Google Drive API v2并发送请求


您可以使用参考指南中的Java示例,并对其进行编辑,使其不包含MediaContent:

根据以下链接,我已经解决了问题:

第二个put请求不需要任何额外的头,例如GData版本:3.0和授权:Bearer****等。唯一需要做的是使用位置URI创建一个新的put请求,然后它将返回带有201状态代码的响应


谢谢大家。

谢谢你们的帮助,克劳迪奥。我正在开发一个android应用程序,GoogleDrive库非常庞大,并且有很多依赖性,这将使我的应用程序大小加倍。这就是我学习直接使用文档API的原因。此外,驱动器API每天的配额为500000个请求。如果需要,您可以从API控制台请求更多配额,我们希望了解更多有关您产品的信息: