更新使用API​​的谷歌文档文件? [英] Updating a document in Google Docs using the API?

查看:93
本文介绍了更新使用API​​的谷歌文档文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新已上传的谷歌文档文件的内容。 我使用的是下面的code:

I want to update the contents of the already uploaded Google Doc file. I'm using the below code:

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我试图用一切可能的环节:交替,自我,编辑,编辑,媒体甚至可恢复编辑媒体,但​​我一直在得到异常

For "feedURL" I tried using all the possible links: alternate, self, edit, edit-media even resumable-edit-media, but I keep on getting exceptions.

另外我怎么看这样的请求的响应?

Also how do I read a response with such requests?

我刚开始使用这个API。此前,我用它在协议层面就这么被发送GET / POST请求和接收网络响应。我不知道如何获得或读取应答在这种情况下。

I just started using this API. Earlier, I was using it on the protocol level so was sending GET/POST requests and receiving web responses. I don't know how to get or read response in this case.

更新:

现在的code我使用的是:

Now the code I'm using is:

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)precondition失败}

And the exception I'm getting is: {"The remote server returned an error: (412) Precondition Failed."} I'm getting this exception at DocumentEntry entry = request.Service.Update(mediaUri, textStream, DocumentContentType, title) as DocumentEntry;

推荐答案

解决..异常precondition到Etag的不匹配失败是由于 上述修订code完美的作品来保存文件。

Solved.. Exception precondition Failed was due to Etag mismatch The above UPDATED code works perfectly for saving a document.

这篇关于更新使用API​​的谷歌文档文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆