【问题标题】:Uploading Any File To Google Docs Using GData使用 GData 将任何文件上传到 Google Docs
【发布时间】:2012-04-23 02:55:39
【问题描述】:

我正在尝试使用 Google Docs GData API (.NET) 将文件上传到我的文档,但我不断收到错误消息。我找不到任何使用这种方法的例子,所以我什至不确定我是否正确使用它。

DocumentsService docService = new DocumentsService("MyDocsTest");
docService.setUserCredentials("w****", "*****");

DocumentsListQuery docQuery = new DocumentsListQuery();
DocumentsFeed docFeed = docService.Query(docQuery);

foreach (DocumentEntry entry in docFeed.Entries)
{
    Console.WriteLine(entry.Title.Text);
}

Console.ReadKey();
Console.WriteLine();

if (File.Exists(@"testDoc.txt") == false)
{
    File.WriteAllText(@"testDoc.txt", "test");
}

docService.UploadDocument(@"testDoc.txt", null); // Works Fine
docService.UploadFile(@"testDoc.txt", null, @"text/plain", false); // Throws Error

上面的代码会抛出一个GDataRequestException:

Execution of request failed: https://docs.google.com/feeds/default/private/full?convert=false

这有点令人恼火,因为这个 API 可能非常有用。有谁知道我做错了什么?

【问题讨论】:

    标签: c# .net gdata gdata-api google-docs-api


    【解决方案1】:

    经过大量的实验和研究,我得到了它的工作。在我的困境中,将把这个留给其他人。我将留下使用简写以供参考。

    // Start the service and set credentials
    Docs.DocumentsService service = new Docs.DocumentsService("GoogleApiTest");
    service.setUserCredentials("username", "password");
    
    // Initialize the DocumentEntry
    Docs.DocumentEntry newEntry = new Docs.DocumentEntry();
    newEntry.Title = new Client.AtomTextConstruct(Client.AtomTextConstructElementType.Title, "Test Upload"); // Set the title
    newEntry.Summary = new Client.AtomTextConstruct(Client.AtomTextConstructElementType.Summary ,"A summary goes here."); // Set the summary
    newEntry.Authors.Add(new Client.AtomPerson(Client.AtomPersonType.Author, "A Person")); // Add a main author
    newEntry.Contributors.Add(new Client.AtomPerson(Client.AtomPersonType.Contributor, "Another Person")); // Add a contributor
    newEntry.MediaSource = new Client.MediaFileSource("testDoc.txt", "text/plain"); // The actual file to be uploading
    
    // Create an authenticator
    Client.ClientLoginAuthenticator authenticator = new Client.ClientLoginAuthenticator("GoogleApiTest", Client.ServiceNames.Documents, service.Credentials);
    
    // Setup the uploader
    Client.ResumableUpload.ResumableUploader uploader = new Client.ResumableUpload.ResumableUploader(512);
    uploader.AsyncOperationProgress += (object sender, Client.AsyncOperationProgressEventArgs e) =>
        {
            Console.WriteLine(e.ProgressPercentage + "%"); // Progress updates
        };
    uploader.AsyncOperationCompleted += (object sender, Client.AsyncOperationCompletedEventArgs e) =>
        {
            Console.WriteLine("Upload Complete!"); // Progress Completion Notification
        };
    
    Uri uploadUri = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false"); // "?convert=false" makes the doc be just a file
    Client.AtomLink link = new Client.AtomLink(uploadUri.AbsoluteUri);
    link.Rel = Client.ResumableUpload.ResumableUploader.CreateMediaRelation;
    newEntry.Links.Add(link);
    
    uploader.InsertAsync(authenticator, newEntry, new object()); // Finally upload the bloody thing
    

    【讨论】:

    • 太棒了,这与developers.google.com/gdata/docs/… 的(几乎相同的)样本一起极大地帮助了我。我觉得我应该添加的另一件事(因为我在任何地方都找不到任何示例)是,如果您使用的是 OAuth2,则需要将 ClientLoginAuthenticator 实例交换为 OAuth2Authenticator 实例。
    【解决方案2】:

    您能否检查正在抛出的 GDataRequestException 的 ResponseString 属性以获得详细的错误消息?

    在尝试调试此类问题时,使用 Fiddler 等工具捕获您的请求也会有很大帮助。

    【讨论】:

    • 上面写着:<errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>ServiceForbiddenException</code><internalReason>Files must be uploaded using the resumable upload mechanism.</internalReason></error></errors> 什么是可恢复上传机制?
    • developers.google.com/google-apps/documents-list/… 可恢复上传的 .NET 示例即将添加
    • 想通了。感谢您的帮助:D
    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 2012-06-17
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    相关资源
    最近更新 更多