【问题标题】:Updating a document in Google Docs using the API?使用 API 更新 Google Docs 中的文档?
【发布时间】:2012-04-14 22:18:21
【问题描述】:

我想更新已上传的 Google Doc 文件的内容。 我正在使用以下代码:

DocumentsService service = new DocumentsService("app-v1");
string auth = gLogin2();
service.SetAuthenticationToken(auth);
Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(
   "CONTENTS PLEASE CHANGE"));
DocumentEntry entry = service.Update(new Uri("feedURL"), stream, "text/plain", 
    "nameOfDoc") as DocumentEntry;

对于“feedURL”,我尝试使用所有可能的链接:alternate、self、edit、edit-media 甚至 resumable-edit-media,但我不断收到异常。

另外,我如何阅读此类请求的响应?

我刚开始使用这个 API。早些时候,我在协议级别使用它,因此发送 GET/POST 请求和接收 Web 响应。在这种情况下,我不知道如何获取或读取响应。

更新:

现在我使用的代码是:

RequestSettings _settings;
            string DocumentContentType = "text/html";
            _settings = new RequestSettings("Stickies", "EMAIL", "PASSWORD");
            var request = new DocumentsRequest(_settings);


            //var entryToUpdate = doc.DocumentEntry;
            var updatedContent = "new content..."; ;

            var mediaUri = new Uri(string.Format(DocumentsListQuery.mediaUriTemplate, rid));
            Trace.WriteLine(mediaUri);
            var textStream = new MemoryStream(Encoding.UTF8.GetBytes(updatedContent));

            var reqFactory = (GDataRequestFactory)request.Service.RequestFactory;
            reqFactory.CustomHeaders.Add(string.Format("{0}: {1}", GDataRequestFactory.IfMatch, et));
            var oldEtag = et;
            DocumentEntry entry = request.Service.Update(mediaUri, textStream, DocumentContentType, title) as DocumentEntry;
            Debug.WriteLine(string.Format("ETag changed while saving {0}: {1} -> {2}", title, oldEtag,et));
            Trace.WriteLine("reached");

我得到的例外是: {“远程服务器返回错误:(412)前提条件失败。”} 我在DocumentEntry entry = request.Service.Update(mediaUri, textStream, DocumentContentType, title) as DocumentEntry;

收到此异常

【问题讨论】:

  • @AliAfshar 我想即使上面的代码也不正确。因为我认为我必须为此创建一个文档请求..

标签: c# .net google-docs google-docs-api


【解决方案1】:

已解决.. 异常前提条件失败是由于 Etag 不匹配 上面的 UPDATED 代码非常适合保存文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多