2011-04-22 30 views
0

我在這一點上有一個非常簡單的MVC應用程序:
控制器:正確進行身份驗證來獲得在谷歌聯繫人...(C#/ MVC3)

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     ViewBag.Message = "Welcome!"; 

     var qs = HttpContext.Request.QueryString; 
     var keys = qs.AllKeys.ToList(); 
     if (keys.Count > 0 && keys.Contains("token")) 
     { 
      Session["token"] = qs.Get("token"); 
      Models.GoogleContact gc = new Models.GoogleContact(); 
     } 
     else 
     { 
      ViewBag.GoogleUrl = AuthSubUtil.getRequestUrl(HttpContext.Request.Url.AbsoluteUri, "https://www.google.com/m8/feeds/", false, true); 
     } 

     return View(); 
    } 

    public ActionResult About() 
    { 
     return View(); 
    } 
} 

我有這個主頁查看

@{ 
    ViewBag.Title = "Home Page"; 
} 

<p>Home Page...</p> 

<a href="@ViewBag.GoogleUrl">Tie in with Google</a> 
<br /> 
<br /> 

當應用第一次啓動時沒有查詢字符串,所以控制器將創建我在主頁上嵌入的鏈接。您點擊該鏈接並將其發送給Google。授權您希望此應用有權訪問Google通訊錄,並返回查詢字符串返回主頁。控制器看到查詢字符串,剝離令牌並實例化Google「模型」類。

基類:

internal class baseGoogle 
{ 
    #region Private Properties 
    internal const string googleContactToken = "cp"; 
    internal const string googleCalendarToken = "cl"; 
    internal string _authSubToken; 
    internal GAuthSubRequestFactory _gAuthSubRequestFactory; 
    internal RequestSettings _requestSettings; 
    internal ContactsRequest _contactsRequest; 
    internal ContactsService _contactsService; 
    #endregion 

    internal baseGoogle() 
    { 
#if DEBUG 
     _authSubToken = HttpContext.Current.Session["token"].ToString(); 
     _gAuthSubRequestFactory = new Google.GData.Client.GAuthSubRequestFactory(googleContactToken, "Tester1"); 
     _requestSettings = new Google.GData.Client.RequestSettings(_gAuthSubRequestFactory.ApplicationName, _authSubToken); 
     _contactsRequest = new Google.Contacts.ContactsRequest(_requestSettings); 
     _contactsService = new Google.GData.Contacts.ContactsService(_gAuthSubRequestFactory.ApplicationName); 
     _contactsService.RequestFactory = _gAuthSubRequestFactory; 
#endif 
    } 
} 

谷歌聯繫人類別:

internal class GoogleContact : baseGoogle 
{ 
    #region Public Properties 
    [NotMapped] 
    public Dictionary<string, Group> Groups { get; set; } 
    #endregion 

    public GoogleContact() : base() 
    { 
     // Get the list of contact groups... 
     _requestSettings.AutoPaging = true; 
     Feed<Group> fg = _contactsRequest.GetGroups(); 

     foreach (Group g in fg.Entries) 
     { 
      this.Groups.Add(g.Title, g); 
     } 

    } 
} 

一切似乎是工作的罰款,直到我試圖遍歷飼料項目。它會彈出一個401 - 未授權的錯誤。

爲什麼會這樣做的任何想法?我正在關注Google Dev上的文檔。

我正在使用API​​的1.7.0.1版本。


注:我發現了一個blog entry一些不同的代碼,你猜怎麼着,那工作。現在要弄清楚爲什麼半官方的方式不起作用!任何人有任何想法?

+0

任何最終解決方案與完整的源代碼示例工作呢? – Kiquenet 2013-12-27 12:15:37

回答

2

經過大量的玩耍,我發現了一種方法來完成我所需要的。首先,我不能發現一種方法來使用OpenID授權令牌來獲取該用戶的聯繫人,日曆等我仍然在尋找一種方法來做到這一點。 (see my question here

我弄清楚的是讓一個人在他的個人資料中輸入他的谷歌用戶名和密碼,然後用它通過GData連接到他們的信息。我的感覺是大多數人不想這樣做! (!它是相當冗餘)

這裏是我想出了代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Google.GData.Contacts; 
using Google.GData.Extensions; 

namespace myNameSpace 
{ 
    /// 
    /// Summary description for GoogleContactService 
    /// 
    public class GoogleContactService 
    { 
     #region Properties 
     public static ContactsService GContactService = null; 
     #endregion 

     #region Methods 
     public static void InitializeService(string username, string password) 
     { 
      GContactService = new ContactsService("Contact Infomation"); 
      GContactService.setUserCredentials(username, password); 
     } 
     public static List<ContactDetail> GetContacts(string GroupName = null) 
     { 
      List<ContactDetail> contactDetails = new List<ContactDetail>(); 
      ContactsQuery contactQuery = new ContactsQuery(ContactsQuery.CreateContactsUri("default")); 
      contactQuery.NumberToRetrieve = 1000; 

      if (!String.IsNullOrEmpty(GroupName)) 
      { 
       GroupEntry ge = GetGroup(GroupName); 
       if (ge != null) 
        contactQuery.Group = ge.Id.AbsoluteUri; 
      } 
      else 
      { 
       string groupName = ""; 
       GroupEntry ge = GetGroup(groupName); 
       if (ge != null) 
        contactQuery.Group = ge.Id.AbsoluteUri; 
      } 

      ContactsFeed feed = GContactService.Query(contactQuery); 
      foreach (ContactEntry entry in feed.Entries) 
      { 
       ContactDetail contact = new ContactDetail 
       { 
        Name = entry.Title.Text, 
        EmailAddress1 = entry.Emails.Count >= 1 ? entry.Emails[0].Address : "", 
        EmailAddress2 = entry.Emails.Count >= 2 ? entry.Emails[1].Address : "", 
        Phone1 = entry.Phonenumbers.Count >= 1 ? entry.Phonenumbers[0].Value : "", 
        Phone2 = entry.Phonenumbers.Count >= 2 ? entry.Phonenumbers[1].Value : "", 
        Address = entry.PostalAddresses.Count >= 1 ? entry.PostalAddresses[0].FormattedAddress : "", 
        Details = entry.Content.Content 
       }; 

       contact.UserDefinedFields = new List<UDT>(); 
       foreach (var udt in entry.UserDefinedFields) 
       { 
        contact.UserDefinedFields.Add(new UDT { Key = udt.Key, Value = udt.Value }); 
       } 

       contactDetails.Add(contact); 
      } 

      return contactDetails; 
     } 
     #endregion 

     #region Helpers 
     private static GroupEntry GetGroup(string GroupName) 
     { 
      GroupEntry groupEntry = null; 

      GroupsQuery groupQuery = new GroupsQuery(GroupsQuery.CreateGroupsUri("default")); 
      groupQuery.NumberToRetrieve = 100; 
      GroupsFeed groupFeed = GContactService.Query(groupQuery); 
      foreach (GroupEntry entry in groupFeed.Entries) 
      { 
       if (entry.Title.Text.Equals(GroupName, StringComparison.CurrentCultureIgnoreCase)) 
       { 
        groupEntry = entry; 
        break; 
       } 
      } 
      return groupEntry; 
     } 
     #endregion 
    } 

    public class ContactDetail 
    { 
     public string Name { get; set; } 
     public string EmailAddress1 { get; set; } 
     public string EmailAddress2 { get; set; } 
     public string Phone1 { get; set; } 
     public string Phone2 { get; set; } 
     public string Address { get; set; } 
     public string Details { get; set; } 
     public string Pipe { get; set; } 
     public string Relationship { get; set; } 
     public string Status { get; set; } 
     public List<UDT> UserDefinedFields { get; set; } 
    } 
    public class UDT 
    { 
     public string Key { get; set; } 
     public string Value { get; set; } 
    } 
} 

現在,爲了弄清楚如何使用他們的OpenID登錄,而不必要求他們的登錄憑證!