C# 使用API更新Google文档中的文档?
我想更新已经上传的谷歌文档文件的内容。 我正在使用以下代码:C# 使用API更新Google文档中的文档?,c#,.net,google-docs,google-docs-api,C#,.net,Google Docs,Google Docs Api,我想更新已经上传的谷歌文档文件的内容。 我正在使用以下代码: DocumentsService service = new DocumentsService("app-v1"); string auth = gLogin2(); service.SetAuthenticationToken(auth); Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes( "CONTENTS PLEASE CHANGE")); D
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”,我尝试使用所有可能的链接:备用、自我、编辑、编辑媒体甚至可恢复的编辑媒体,但我不断遇到例外
还有,我如何阅读此类请求的响应
我刚开始使用这个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)作为DocumentEntry得到这个异常代码>已解决。。由于Etag不匹配,异常前提条件失败
以上更新的代码非常适合保存文档。已解决。。由于Etag不匹配,异常前提条件失败
上面更新的代码非常适合保存文档。@AliAfshar我想即使上面的代码也不正确。因为我想我必须为此创建一个文档请求。@AliAfshar我想即使是上面的代码也不正确。我想我必须为此创建一个文档请求。。