2011-10-14 75 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二进制文件。虽然我还没有创建一个新的文档,但我确实使用类似的类来创建日历项目。