2012-06-09 18 views
0

我想通過Google文檔列表API(C#)中的System.IO.FileStream上傳/更新文件嗎?

我使用下面雙向:Google.GData.Client.ResumableUpload.ResumableUploader
(1)公共無效UpdateAsync(身份驗證的身份驗證,AbstractEntry有效載荷,對象的UserData)
(2)公共無效UpdateAsync(身份驗證的身份驗證,烏里resumableUploadUri,流有效載荷,字符串contentType,對象userData)

(1)成功。
(2)失敗,403禁止或其他...

那麼,有人有關於(2)的任何示例代碼嗎?
如何上傳/更新FileStream和C#中的ResumableUploader文件

我的代碼(2): 此代碼是由示例代碼編輯從克勞迪奧凱魯比諾,它的上傳文件流谷歌文檔(驅動器)的工作。但是文件(DocumentEntry)的名稱在Google雲端硬盤頁面上顯示爲「無標題」。看起來'slu''不起作用。我錯過了一些重要的設置?

Google.GData.Documents.DocumentsService conn = 
new Google.GData.Documents.DocumentsService("TestSend"); 
conn.setUserCredentials("UserName", "UserPass"); 

string path = @"D:\test_file\test.exe"; 

Google.GData.Client.ResumableUpload.ResumableUploader send = 
new Google.GData.Client.ResumableUpload.ResumableUploader(); 
send.AsyncOperationCompleted += new Google.GData.Client.AsyncOperationCompletedEventHandler(
    delegate(object sender, 
     Google.GData.Client.AsyncOperationCompletedEventArgs e) 
    { 
     System.Windows.Forms.MessageBox.Show("File Send Done"); 
    } 
); 

System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite); 

send.InsertAsync(
    new Google.GData.Client.ClientLoginAuthenticator("TestSend", "writely", this._DiskConn.Credentials), 
    new System.Uri("https://docs.google.com/feeds/upload/create-session/default/private/full?v=3"), 
    fs, 
    "application/octet-stream", 
    System.IO.Path.GetFileName(path), 
    new object() 
); 

回答

0

試試下面的代碼:

DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1"); 
ClientLoginAuthenticator authenticator = new ClientLoginAuthenticator(APPLICATION_NAME, ServiceNames.Documents, USERNAME, PASSWORD); 

string slug = "Legal contract"; 
MediaFileSource mediaSource = new MediaFileSource("c:\\contract.txt", "text/plain"); 
Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full?v=3"); 

ResumableUploader ru = new ResumableUploader(); 
ru.InsertAsync(authenticator, createUploadUrl, mediaSource.GetDataStream(), mediaSource.ContentType, slug, new object()); 
+0

它的工作!我將'mediaSource.GetDataStream()'替換爲'新的FileStream(「[文件路徑]」,FileMode.Open)',並且我在Google Drive上找到了該文件。但有點奇怪,在Google Drive中名爲「無標題」的DocumentEntry,slug無法正常工作。我用我的新代碼上傳我的帖子。 – user1446337

相關問題