2011-09-19 55 views
1

工作平臺:ASP.NET 4.0 C#(框架不可知論)訪問谷歌文檔與的GData

谷歌的GData是我的依賴

using Google.GData.Client; 
using Google.GData.Extensions; 
using Google.GData.Documents; 

我有兩個頁面驗證和列表。

驗證重定向到谷歌服務器這樣

public ActionResult Auth() 
{ 
    var target = Request.Url.ToString().ToLowerInvariant().Replace("auth", "list"); 
    var scope = "https://docs.google.com/feeds/"; 
    bool secure = false, session = true; 

    var authSubUrl = AuthSubUtil.getRequestUrl(target, scope, secure, session); 
    return new RedirectResult(authSubUrl); 
} 

現在到達列表頁面,如果認證成功。

public ActionResult List() 
{ 
    if (Request.QueryString["token"] != null) 
    { 
     String singleUseToken = Request.QueryString["token"]; 

     string consumerKey = "www.blahblah.net"; 
     string consumerSecret = "my_key"; 
     string sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, null).ToString(); 

     var authFactory = new GOAuthRequestFactory("writely", "qwd-asd-01"); 
     authFactory.Token = sessionToken; 
     authFactory.ConsumerKey = consumerKey; 
     authFactory.ConsumerSecret = consumerSecret; 
     //authFactory.TokenSecret = ""; 
     try 
     { 
      var service = new DocumentsService(authFactory.ApplicationName) { RequestFactory = authFactory }; 

      var query = new DocumentsListQuery(); 
      query.Title = "project"; 

      var feed = service.Query(query); 
      var result = feed.Entries.ToList().ConvertAll(a => a.Title.Text); 
      return View(result); 
     } 
     catch (GDataRequestException gdre) 
     { 
      throw; 
     } 
    } 
} 

這種失敗在與錯誤的行var feed = service.Query(query);

的要求執行失敗:https://docs.google.com/feeds/default/private/full?title=project

收到catch塊上的HttpStatusCodeHttpStatusCode.Unauthorized

什麼是錯的用這個代碼?我需要獲取TokenSecret嗎?如果是這樣如何?

+0

您是否測試了您嘗試使用客戶端身份驗證執行的查詢?只是爲了排除該部分的任何錯誤。 –

回答

1

使用的3-legged OAuth in the Google Data Protocol Client Libraries

示例代碼

string CONSUMER_KEY = "www.bherila.net"; 
string CONSUMER_SECRET = "RpKF7ykWt8C6At74TR4_wyIb"; 
string APPLICATION_NAME = "bwh-wssearch-01"; 

string SCOPE = "https://docs.google.com/feeds/"; 

public ActionResult Auth() 
{ 
    string callbackURL = String.Format("{0}{1}", Request.Url.ToString(), "List"); 
    OAuthParameters parameters = new OAuthParameters() 
    { 
     ConsumerKey = CONSUMER_KEY, 
     ConsumerSecret = CONSUMER_SECRET, 
     Scope = SCOPE, 
     Callback = callbackURL, 
     SignatureMethod = "HMAC-SHA1" 
    }; 

    OAuthUtil.GetUnauthorizedRequestToken(parameters); 
    string authorizationUrl = OAuthUtil.CreateUserAuthorizationUrl(parameters); 
    Session["parameters"] = parameters; 
    ViewBag.AuthUrl = authorizationUrl; 
    return View(); 
} 

public ActionResult List() 
{ 
    if (Session["parameters"] != null) 
    { 
     OAuthParameters parameters = Session["parameters"] as OAuthParameters; 
     OAuthUtil.UpdateOAuthParametersFromCallback(Request.Url.Query, parameters); 

     try 
     { 
      OAuthUtil.GetAccessToken(parameters); 

      GOAuthRequestFactory authFactory = new GOAuthRequestFactory("writely", APPLICATION_NAME, parameters); 

      var service = new DocumentsService(authFactory.ApplicationName); 
      service.RequestFactory = authFactory; 

      var query = new DocumentsListQuery(); 
      //query.Title = "recipe"; 

      var feed = service.Query(query); 
      var docs = new List<string>(); 
      foreach (DocumentEntry entry in feed.Entries) 
      { 
       docs.Add(entry.Title.Text); 
      } 
      //var result = feed.Entries.ToList().ConvertAll(a => a.Title.Text); 
      return View(docs); 
     } 
     catch (GDataRequestException gdre) 
     { 
      HttpWebResponse response = (HttpWebResponse)gdre.Response; 

      //bad auth token, clear session and refresh the page 
      if (response.StatusCode == HttpStatusCode.Unauthorized) 
      { 
       Session.Clear(); 
       Response.Write(gdre.Message); 
      } 
      else 
      { 
       Response.Write("Error processing request: " + gdre.ToString()); 
      } 
      throw; 
     } 
    } 
    else 
    { 
     return RedirectToAction("Index"); 
    } 
} 

This 2-legged sample從來沒有爲我工作的谷歌文檔。

+0

如果三足動物有效,只需將自己的答案標記爲「答案」即可。 –

+0

@Christophe Geers:我非常清楚這一點。我在這裏度過了很多時間。回答自己的問題只會在截至目前的兩天後纔有效:) – naveen

+0

好吧,我的不好。還不知道2天的延遲。 –

1

您需要向Google請求令牌並使用它來初始化您的DocumentsService實例。

下面是使用Google ContactsService的示例。 DocumentsService應該是相同的。

Service service = new ContactsService("My Contacts Application"); 
service.setUserCredentials("[email protected]", "yourpassword"); 
var token = service.QueryClientLoginToken(); 
service.SetAuthenticationToken(token); 

但正如您所提到的,您正在使用AuthSub。我跳得太快了。

我看到您正在請求會話令牌。根據API的文檔,您必須使用會話令牌通過將令牌放置在授權標頭中來驗證對服務的請求。設置會話令牌後,您可以使用Google Data APIs客戶端庫。

下面是關於如何使用的AuthSub與.NET客戶端庫一個完整的例子(由谷歌):

http://code.google.com/intl/nl-NL/apis/gdata/articles/authsub_dotnet.html

讓我有縮短的例子:

GAuthSubRequestFactory authFactory = 
    new GAuthSubRequestFactory("cl", "TesterApp"); 
authFactory.Token = (String) Session["token"]; 
CalendarService service = new CalendarService(authFactory.ApplicationName); 
service.RequestFactory = authFactory; 

EventQuery query = new EventQuery(); 

query.Uri = new Uri("http://www.google.com/calendar/feeds/default/private/full"); 

EventFeed calFeed = service.Query(query); 
foreach (Google.GData.Calendar.EventEntry entry in calFeed.Entries) 
{ 
    //... 
} 

如果我正確地看到你的示例代碼非常遵循相同的步驟,不同的是你爲AuthFactory設置了ConsumerKey和ConsumerSecret,這在Google的例子中沒有完成。

+0

謝謝。我沒有使用客戶端登錄。我知道如何使用它。 – naveen

+0

好的,我的壞。更新了我的答案。 –

+0

讓我檢查一下。 – naveen