2012-04-14 100 views
2

我想更新已上傳的Google Doc文件的內容。 我使用下面的代碼:使用API​​更新Google文檔中的文檔?

DocumentsService service = new DocumentsService("app-v1"); 
string auth = gLogin2(); 
service.SetAuthenticationToken(auth); 
Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(
    "CONTENTS PLEASE CHANGE")); 
DocumentEntry entry = service.Update(new Uri("feedURL"), stream, "text/plain", 
    "nameOfDoc") as DocumentEntry; 

對於「feedURL」我嘗試使用所有可能的鏈接:交替,自我,編輯,編輯,媒體甚至可恢復編輯媒體,但我不斷收到異常。

另外我如何閱讀這樣的請求?

我剛開始使用這個API。之前,我在協議級別使用它,因此發送GET/POST請求並接收Web響應。在這種情況下,我不知道如何獲取或閱讀回覆。

UPDATE:

現在我正在使用的代碼是:

RequestSettings _settings; 
      string DocumentContentType = "text/html"; 
      _settings = new RequestSettings("Stickies", "EMAIL", "PASSWORD"); 
      var request = new DocumentsRequest(_settings); 


      //var entryToUpdate = doc.DocumentEntry; 
      var updatedContent = "new content..."; ; 

      var mediaUri = new Uri(string.Format(DocumentsListQuery.mediaUriTemplate, rid)); 
      Trace.WriteLine(mediaUri); 
      var textStream = new MemoryStream(Encoding.UTF8.GetBytes(updatedContent)); 

      var reqFactory = (GDataRequestFactory)request.Service.RequestFactory; 
      reqFactory.CustomHeaders.Add(string.Format("{0}: {1}", GDataRequestFactory.IfMatch, et)); 
      var oldEtag = et; 
      DocumentEntry entry = request.Service.Update(mediaUri, textStream, DocumentContentType, title) as DocumentEntry; 
      Debug.WriteLine(string.Format("ETag changed while saving {0}: {1} -> {2}", title, oldEtag,et)); 
      Trace.WriteLine("reached"); 

而且我得到的例外是: {「遠程服務器返回錯誤:(412)預處理失敗「} 我得到這個例外在DocumentEntry entry = request.Service.Update(mediaUri, textStream, DocumentContentType, title) as DocumentEntry;

+0

請添加您的例外。 – 2012-04-15 14:01:42

+0

@AliAfshar我想即使上面的代碼是不正確的。因爲我想我必須爲此創建一個Document請求.. – 2012-04-15 14:09:17

回答

0

解決..異常前提失敗是由於Etag的不匹配 上面更新的代碼完全適用於保存文檔。