2012-04-10 7 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の(ほぼ同じ)サンプルと一緒に私を大きく助けました。追加する必要があるもう1つの事柄は(私がどこでもサンプルを見つけることができなかったため)、代わりにOAuth2を使用している場合、ClientLoginAuthenticatorインスタンスをOAuth2Authenticatorのインスタンスに交換する必要があるということです。 –

0

詳細なエラーメッセージを表示するためにスローされている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

関連する問題