使用谷歌 API 上传任何文件

本文关键字:任何 文件 API 谷歌 | 更新日期: 2023-09-27 17:56:02

根据谷歌博客,我能够使用谷歌API将任何类型的文件上传到我的个人文档中(对于企业帐户来说已经是可能的)。

但是现在,当我尝试将?convert=false添加到 URI 时,我收到了来自服务的 403 响应。

我使用这段代码(它使用来自SVN主干的google-gdata C# api客户端)

int CHUNK_SIZE = 1;
ClientLoginAuthenticator cla = new ClientLoginAuthenticator("uploader", ServiceNames.Documents, "username", "password");
// Set up resumable uploader and notifications
ResumableUploader ru = new ResumableUploader(CHUNK_SIZE);
ru.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(ru_AsyncOperationCompleted);
ru.AsyncOperationProgress += new AsyncOperationProgressEventHandler(ru_AsyncOperationProgress);
// Set metadata for our upload.
Document entry = new Document();
entry.Title = "test file";
entry.MediaSource = new MediaFileSource(fileName, "application/msword");
// Add the upload uri to document entry.
Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false");
AtomLink link = new AtomLink(createUploadUrl.AbsoluteUri);
link.Rel = ResumableUploader.CreateMediaRelation;
entry.DocumentEntry.Links.Add(link);
ru.InsertAsync(cla, entry.DocumentEntry, new object());

没有?convert=false它工作正常。

我很感激任何帮助:例如,如何查看API客户端实际从服务中发布/接收的内容。

使用谷歌 API 上传任何文件

更新的文档现在包含一个完整的示例,演示如何使用 ResumableUploader 组件上传任何文件: https://developers.google.com/google-apps/documents-list/#uploading_a_new_document_or_file_with_both_metadata_and_content

如果这仍然不起作用,我建议安装 Fiddler 来捕获 HTTP 流量并检查请求和响应。