3

我正在尝试将一个简单的文本文件上传到谷歌文档中的特定文件夹,但没有运气。

FileStream fileStream = new FileStream(@"c:\test.txt", System.IO.FileMode.Open);
DocumentEntry lastUploadEntry = 
    globalData.service.UploadDocument("c:\\test.txt", null);                                                                               

string feed =
    "https://docs.google.com/feeds/upload/create-session/default/private/full/folder%folder:0B2dzFB6YvN-kYTRlNmNhYjEtMTVmNC00ZThkLThiMjQtMzFhZmMzOGE2ZWU1/contents/";

var result = 
    globalData.service.Insert(new Uri(feed), fileStream, "application/pdf", "test");

我收到一条错误消息

“远程服务器返回错误:(503) 服务器不可用。”

我怀疑目标文件夹 uri 是错误的,但我无法找出正确的文件夹。

4

3 回答 3

2

https://developers.google.com/google-apps/documents-list/#uploading_a_new_document_or_file_with_both_metadata_and_content有一个使用可恢复上传组件的完整示例:

using System;
using Google.GData.Client;
using Google.GData.Client.ResumableUpload;
using Google.GData.Documents;

namespace MyDocumentsListIntegration
{
  class Program
  {
    static void Main(string[] args)
    {
      DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");

      // TODO: Instantiate an Authenticator object according to your authentication
      // mechanism (e.g. OAuth2Authenticator).
      // Authenticator authenticator =  ...

      // Instantiate a DocumentEntry object to be inserted.
      DocumentEntry entry = new DocumentEntry();

      // Set the document title
      entry.Title.Text = "Legal Contract";

      // Set the media source
      entry.MediaSource = new MediaFileSource("c:\\contract.txt", "text/plain");

      // Define the resumable upload link
      Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full");
      AtomLink link = new AtomLink(createUploadUrl.AbsoluteUri);
      link.Rel = ResumableUploader.CreateMediaRelation;
      entry.Links.Add(link);

      // Set the service to be used to parse the returned entry
      entry.Service = service;

      // Instantiate the ResumableUploader component.
      ResumableUploader uploader = new ResumableUploader();

      // Set the handlers for the completion and progress events
      uploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(OnDone);
      uploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(OnProgress);

      // Start the upload process
      uploader.InsertAsync(authenticator, entry, new object());
    }

    static void OnDone(object sender, AsyncOperationCompletedEventArgs e) {
        DocumentEntry entry = e.Entry as DocumentEntry;
    }

    static void OnProgress(object sender, AsyncOperationProgressEventArgs e) {
        int percentage = e.ProgressPercentage;
    }
  }
}
于 2012-04-18T20:28:42.260 回答
1

只需按照文章Google Apps Platform Uploading documents
另请查看Google Documents List API 3.0 版
Uri 应该类似于以下内容:

string feed = @"https://developers.google.com/google-apps/documents-list/#getting_a_resource_entry_again"; 
//it may not be exact, just check and read from the links 
于 2012-04-18T19:21:50.540 回答
1

试试这个 uri:

"https://docs.google.com/feeds/default/private/full/folder%3A" + fRid + "/contents" 

//fRid 是文件夹的资源 ID。在您的情况下:0B2dzFB6YvN-kYTRlNmNhYjEtMTVmNC00ZThkLThiMjQtMzFhZmMzOGE2ZWU1

另外我猜你的 URI 给出了这个错误,因为你使用文件夹资源 ID 作为 - folder:resourceID

尝试删除文件夹:并仅使用 RID

剪切“文件夹:”的代码 -

int ridIndex = dRid.IndexOf(":");
Rid = Rid.Substring(ridIndex + 1);
于 2012-04-21T06:00:50.827 回答