2012-08-31 43 views
2

我身邊讀了幾個問題,我想這些例子,他們的工作對我來說,這裏是鏈接: - Link 1 - Link 2 - Link 3C#谷歌阿比載流

然而, 我是什麼嘗試完成它有點不同,即時嘗試修改/上傳包含純文本里面的.DAT文件。我能夠毫無問題地閱讀文件內容,但我總是得到400錯誤請求(協議錯誤)。繼承人我的代碼。

DocumentsService service = new DocumentsService("Man"); 
    service.setUserCredentials(UserName, Passwod); 

    DocumentsListQuery fileQuery = new DocumentsListQuery(); 
    fileQuery.Query = User.FileName; 
    fileQuery.TitleExact = true; 
    DocumentsFeed feed = service.Query(fileQuery); 

    //Here I get my file to update 
    DocumentEntry entry = (DocumentEntry)feed.Entries[0]; 

    // Set the media source 
    //Here I have tried application/octet-stream also 
    entry.MediaSource = new MediaFileSource(DataStream, User.FileName, text/plain"); 

    // Instantiate the ResumableUploader component. 
    ResumableUploader uploader = new ResumableUploader(); 

    // Set the handlers for the completion and progress events 
    uploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(OnDone); 
    uploader.AsyncOperationProgress += new syncOperationProgressEventHandler(OnProgress); 

ClientLoginAuthenticator authenticator = new ClientLoginAuthenticator("Man", ServiceNames.Documents,Credentials); 
Uri updateUploadUrl = new Uri(UploadUrl); 
AtomLink aLink = new AtomLink(updateUploadUrl.AbsoluteUri); 
aLink.Rel = ResumableUploader.CreateMediaRelation; 
entry.Links.Add(aLink); 

// Start the update process. 
uploader.UpdateAsync(authenticator,entry,new object()); 

謝謝。

編輯: 這就是我解決它的方法。感謝克勞迪奧爲指導我在正確的方向


  1. 下載應用實例來源:Download Sample (.zip)
  2. 實現從SampleHelper項目,這些項目中:AuthorizationMgr,INativeAuthorizationFlow,LoopbackServerAuthorizationFlow,WindowTitleNativeAuthorizationFlow
  3. 使用此代碼:

    //Call Authorization method... (omitted) 
    
    File body = new File(); 
    body.Title = title; 
    body.Description = description; 
    body.MimeType = mimeType; 
    byte[] byteArray = System.IO.File.ReadAllBytes(filename); 
    MemoryStream stream = new MemoryStream(byteArray); 
    
    try { 
        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType); 
        request.Upload(); 
    
        File file = request.ResponseBody; 
    
        // Uncomment the following line to print the File ID. 
        // Console.WriteLine("File ID: " + file.Id); 
    
        return file; 
    } catch (Exception e) { 
        Console.WriteLine("An error occurred: " + e.Message); 
        return null; 
    } 
    

回答

2

每當服務器返回400錯誤請求時,它還會包含一條描述性錯誤消息,告訴您的請求中哪些內容無效。如果你無法從調試器中獲得它,你應該安裝Fiddler並捕獲HTTP請求和響應。

我給你的另一個建議是開始使用新的Drive API而不是文檔列表API。這裏是展示如何將文件上傳到谷歌驅動器有5分鐘的C#快速啓動示例:

https://developers.google.com/drive/quickstart

+0

你知道或雲端的WinForms的例子... – w0rtez

+0

該庫包括了一個WinForms樣本任務API(http://code.google.com/p/google-api-dotnet-client/source/browse?repo=samples#hg%2FTasks.WinForms.NoteMgr),不應該很難將它與我送你的Drive樣品 –

+0

好的。謝謝,我會檢查出來,並讓你知道... – w0rtez