2012-04-10 114 views
0

我試圖使用Google Docs GData API(.NET)將文件上傳到我的文檔,但我不斷收到引發的錯誤。我找不到任何使用此方法的示例,所以我甚至不確定我是否正確地使用它。使用GData將任何文件上傳到Google文檔

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 

這是一種aggrivating的,看到這個API可以如此瘋狂的幫助。有誰知道我做錯了什麼?

回答

2

經過大量的實驗和研究,我得到了它的工作。要在我的困境中將其留給別人。我將留下使用簡寫作參考。

// 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 
+0

輝煌,這在https://developers.google.com/gdata/docs/resumable_upload#InitialRequestDotNet上與(幾乎完全相同)樣本一起幫助非常大。我覺得還應該添加另一件事(因爲我無法在任何地方找到任何示例)是,如果您使用OAuth2,則ClientLoginAuthenticator實例需要交換爲OAuth2Authenticator實例。 – 2012-06-09 20:18:50

0

您可以檢查正在拋出的GDataRequestException的ResponseString屬性以獲取詳細的錯誤消息嗎?

使用像Fiddler這樣的工具捕獲您的請求在嘗試調試這類問題時也會幫助您很多。

+0

它說: '<錯誤的xmlns = 'HTTP://schemas.google.com/g/2005'>的GDataServiceForbiddenException文件必須使用可恢復上傳機制上傳。' 什麼是可恢復上傳機制? – Abion47 2012-04-11 04:59:47

+0

https://developers.google.com/google-apps/documents-list/#creating_and_uploading_documents_and_files 可恢復上傳的.NET樣本將很快添加 – 2012-04-11 16:22:33

+0

算出來。感謝您的幫助:D – Abion47 2012-04-11 19:26:07

相關問題