2011-10-14 113 views
1

我正在編寫一個使用Google文檔和日曆API的.NET WPF應用程序。我打算在後期集成一些較新的API,比如Tasks,所以我現在要開始準備使用OAuth 2.0。我已經能夠獲得並存儲刷新和訪問令牌,並且我已經實現了一些邏輯來在當前令牌到期時檢索更多訪問令牌。不過,我在將文檔上傳到Google文檔時遇到了很多問題。看起來,GData客戶端庫本身不支持OAuth 2.0,而且我也不想遷移到較新的客戶端庫(例如任務),因爲我不想在此階段對DotNetOpenAuth進行依賴。相反,我已經實現了我自己的OAuth2Authenticator,它添加了所需的OAuth 2頭文件,並將其用於GData ResumableUploader。當我嘗試使用ResumableUploader發送上傳文檔的請求時,我收到401未經授權的響應,其中包含令牌無效 - 無效的AuthSub令牌。使用OAuth 2.0返回的.NET Google GData Docs API 401

我正在做這樣的呼籲:

ResumableUploader ru = new ResumableUploader(512); 

Document entry = new Document(); 
entry.Title = documentName; 
entry.MediaSource = new MediaFileSource(localDocumentPath, "application/pdf"); 
entry.Type = Document.DocumentType.PDF; 

Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full"); 
AtomLink link = new AtomLink(createUploadUrl.AbsoluteUri); 
link.Rel = ResumableUploader.CreateMediaRelation; 
entry.DocumentEntry.Links.Add(link); 

ru.Insert(new OAuth2Authenticator("MyApplicationName", "MyAccessToken"), entry.DocumentEntry); 

導致該請求(從小提琴手):

POST https://docs.google.com/feeds/upload/create-session/default/private/full 
HTTP/1.1 Authorization: OAuth sOmeTThing+SomThNig+etc== 
Slug: DOC_0108.pdf 
X-Upload-Content-Type: application/pdf 
X-Upload-Content-Length: 175268 
GData-Version: 3.0 
Host: docs.google.com 
Content-Type: application/atom+xml; charset=UTF-8 
Content-Length: 508 Connection: Keep-Alive 

<?xml version="1.0" encoding="utf-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom" 
xmlns:gd="http://schemas.google.com/g/2005" 
xmlns:docs="http://schemas.google.com/docs/2007"> 
    <title type="text">DOC_0108.pdf</title> 
    <link href="https://docs.google.com/feeds/upload/create-session/default/private/full" 
    rel="http://schemas.google.com/g/2005#resumable-create-media" /> 
    <category term="http://schemas.google.com/docs/2007#pdf" 
    scheme="http://schemas.google.com/g/2005#kind" label="pdf" /> 
</entry> 

和相關的401響應:

HTTP/1.1 401 Unauthorized 
Server: HTTP Upload Server Built on Sep 27 2011 04:44:57 (1317123897) 
WWW-Authenticate: AuthSub realm="http://www.google.com/accounts/AuthSubRequest" 
Content-Type: text/html; charset=UTF-8 
Content-Length: 38 
Date: Thu, 13 Oct 2011 08:45:11 GMT 
Pragma: no-cache 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Cache-Control: no-cache, no-store, must-revalidate 

Token invalid - Invalid AuthSub token. 

我已經tr了因爲API和OAuth 2.0文檔似乎有分歧,我還嘗試將該令牌附加爲查詢字符串(?access_token =和?oauth_token =),但在標題中同時包含'授權:OAuth'和'Authorization:Bearer'所有這些事情都給出了相同的迴應。

我已經通過了所有的Google API和OAuth問題,博客文章和文檔,我可以找到並嘗試執行此調用的多種方法(使用.NET GData API的多個實現,一個REST客戶端NuGet包,我自己的REST客戶端實現等),我無法超越這個問題。

任何幫助將不勝感激。

回答

0

我能夠通過OAuth獲取和創建文檔。一旦我與谷歌認證才能訪問該文檔的範圍,我執行以下操作:

// get the list of available google docs 
     RequestSettings settings = new RequestSettings(kApplicationName); 
     settings.AutoPaging = false; 
     if (settings != null) 
     { 
      DocumentsRequest request = new DocumentsRequest(settings); 
      request.Service.RequestFactory = GetGoogleOAuthFactory(); 
      Feed<Document> feed = request.GetEverything(); 

      List<Document> all = new List<Document>(feed.Entries); 

      // loop through the documents and add them from google 
      foreach (Document entry in all) 
      { 
       // first check to see whether the document has already been selected or not 
       bool found = model.Docs.Any(d => d.GoogleDocID == entry.ResourceId); 
       if (!found) 
       { 
        GoogleDocItem doc = new GoogleDocItem(); 
        doc.GoogleDocID = entry.ResourceId; 
        doc.ETag = entry.ETag; 

        doc.Url = entry.DocumentEntry.AlternateUri.Content; 
        doc.Title = entry.Title; 
        doc.DocType = entry.Type.ToString(); 
        doc.DocTypeID = entry.Type; 

        if (entry.ParentFolders.Count == 0) 
        { 
         // add the doc to the list 
         model.AvailableDocs.Add(doc); 

         // if the doc is a folder, get the children 
         if (doc.DocTypeID == Document.DocumentType.Folder) 
         { 
          AddAllChildrenToFolder(ref doc, entry, all); 
         } 
        } 
       } 
      } 
     } 

public GOAuthRequestFactory GetGoogleOAuthFactory() 
    { 
     // build the base parameters 
     OAuthParameters parameters = new OAuthParameters 
     { 
      ConsumerKey = kConsumerKey, 
      ConsumerSecret = kConsumerSecret 
     }; 

     // check to see if we have saved tokens and set 
     var tokens = (from a in context.GO_GoogleAuthorizeTokens select a); 
     if (tokens.Count() > 0) 
     { 
      GO_GoogleAuthorizeToken token = tokens.First(); 
      parameters.Token = token.Token; 
      parameters.TokenSecret = token.TokenSecret; 
     } 

     // now build the factory 
     return new GOAuthRequestFactory("anyname", kApplicationName, parameters); 
    } 

我貼我的授權樣品在這裏:.net - Google/OAuth 2 - Automatic logon。 DocumentsRequest來自Google的.NET二進制文件。雖然我還沒有創建一個新的文檔,但我確實使用類似的類來創建日曆項目。