无法以Google格式(Json)编写Google文档文档

时间:2019-07-03 20:03:50

标签: google-api google-drive-api google-docs google-docs-api

我使用带有documents.get的Google Docs API来阅读文档,并将返回的Json保存到文件中。

现在,我需要将保存的文件上传回Google。我按照Upload Files - Perform a resumable upload部分中的说明进行操作。但是我的请求失败,并出现400错误的请求错误:

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&fields=id HTTP/1.1
X-Upload-Content-Type: application/vnd.google-apps.document
X-Upload-Content-Length: 11861
User-Agent: My Client
Authorization: Bearer XXXXX
Content-Type: application/json; charset=utf-8
Host: www.googleapis.com
Content-Length: 74
Accept-Encoding: gzip, deflate

{"mimeType":"application/vnd.google-apps.document","name":"General1.gdoc"}

HTTP/1.1 200 OK
X-GUploader-UploadID: AEnB2UrINNZDf13bOH9NvoZqXqX1K4reJrqA-0i2mEWNUJaSJKJVD2gcVd_EE6AP7WmefpQnL8aLu5iROcWBCxnKu3RVHlMuSQ
Location: https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&fields=id&upload_id=AEnB2UrINNZDf13bOH9NvoZqXqX1K4reJrqA-0i2mEWNUJaSJKJVD2gcVd_EE6AP7WmefpQnL8aLu5iROcWBCxnKu3RVHlMuSQ
Vary: Origin
Vary: X-Origin
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Thu, 04 Jul 2019 20:53:06 GMT
Content-Length: 0
Server: UploadServer
Content-Type: text/html; charset=UTF-8
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"


PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&fields=id&upload_id=AEnB2UrINNZDf13bOH9NvoZqXqX1K4reJrqA-0i2mEWNUJaSJKJVD2gcVd_EE6AP7WmefpQnL8aLu5iROcWBCxnKu3RVHlMuSQ HTTP/1.1
User-Agent: My Client
Authorization: Bearer XXXX
Content-Range: bytes 0-11860/11861
Host: www.googleapis.com
Content-Length: 11861
Accept-Encoding: gzip, deflate

{
   "title": "General1",
   "body": {
      "content": [
       {
         "endIndex": 1,
         "sectionBreak": {
           "sectionStyle": {
           "columnSeparatorStyle": "NONE",
           "contentDirection": "LEFT_TO_RIGHT"
           ...
}

HTTP/1.1 400 Bad Request
X-GUploader-UploadID: AEnB2UrINNZDf13bOH9NvoZqXqX1K4reJrqA-0i2mEWNUJaSJKJVD2gcVd_EE6AP7WmefpQnL8aLu5iROcWBCxnKu3RVHlMuSQ
Vary: Origin
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Content-Length: 171
Date: Thu, 04 Jul 2019 20:53:06 GMT
Server: UploadServer
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"

{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}

我还尝试发送带有X-Upload-Content-Type:application / json标头的POST,但是结果是相同的。

任何想法为何上述请求失败?

不幸的是,我无法使用documents.create()函数来上载文档,因为它只能创建没有内容的空文档。

请注意,我无法使用Google导出操作,因为我需要避免文档转换json-> docx-> json,以避免格式中断。

更新: 这是我在C#中的Google Doc下载代码。我的工作只是文件。结果是Google Doc Json存储在文件中:

var gdoc = docsService.Documents.Get("1g0jD2870BOtQ7pw8NNRb__Mlku6V-dlBmVpkiiK7WKg");
using (var gdocStream = gdoc.ExecuteAsStream())
{
    using (var file = new System.IO.FileStream("D:\\General1.gdoc", FileMode.CreateNew))
    {

        gdocStream.CopyTo(file);
    }
}

这是我的上传代码,失败了:

var fileMetadata = new File()
{
    Name = "General1.gdoc",
    MimeType = "application/vnd.google-apps.document"
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("D:\\gdoc.gdoc", System.IO.FileMode.Open))
{
     request = driveService.Files.Create(fileMetadata, stream, "application/vnd.google-apps.document");
     request.Fields = "id";
     var response = request.Upload();
}
var file = request.ResponseBody

0 个答案:

没有答案