gpt4 book ai didi

c# - 使用 API 更新 Google Docs 中的文档?

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:06 24 4
gpt4 key购买 nike

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

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。早些时候,我在协议(protocol)级别使用它,因此发送 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;

处收到此异常

最佳答案

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

关于c# - 使用 API 更新 Google Docs 中的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10157816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com