2012-04-10 3 views
0

Google 문서 도구 GData API (.NET)를 사용하여 파일을 내 문서에 업로드하려고하지만 오류가 계속 발생합니다. 이 메서드를 사용하는 예제를 찾을 수 없으므로 올바르게 사용하고 있는지 확실하지 않습니다.GData를 사용하여 파일을 Google 문서로 업로드

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

이이 API 그렇게 미친 듯이 도움이 될 수있는대로보고, aggrivating의 종류 :

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 발생합니다. 아무도 내가 뭘 잘못하고 있는지 알아?

답변

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

Brilliant, https://developers.google.com/gdata/docs/resumable_upload#InitialRequestDotNet에서 (거의 동일한) 샘플과 함께 크게 도움이되었습니다. 추가해야한다고 생각되는 또 다른 한 가지는 (내가 어디서나 샘플을 찾을 수 없었기 때문에) 대신 OAuth2를 사용하는 경우 ClientLoginAuthenticator 인스턴스를 OAuth2Authenticator의 인스턴스로 바꿔야한다는 것입니다. –

0

자세한 오류 메시지를 얻기 위해 Throw되는 GDataRequestException의 ResponseString 속성을 확인할 수 있습니까?

Fiddler와 같은 도구로 요청을 캡처하면 이러한 종류의 문제를 디버깅 할 때 많은 도움이됩니다.

+0

은 말한다 : '<오류의 xmlns ='HTTP : //schemas.google.com/g/2005 '>의 GDataServiceForbiddenException 파일이 재개 업로드 메커니즘을 사용하여 업로드해야합니다. ' 재개 가능한 업로드 메커니즘은 무엇입니까? – Abion47

+0

https://developers.google.com/google-apps/documents-list/#creating_and_uploading_documents_and_files 재개 가능한 업로드를위한 .NET 샘플이 곧 추가됩니다. –

+0

알아 냈습니다. 도와 주셔서 감사합니다. D – Abion47