2

我想更新已经上传的 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;

4

1 回答 1

0

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

于 2012-04-16T04:53:50.443 回答