2011-04-22 2 views
0

(C 번호/MVC3) :
컨트롤러 :... 나는이 시점에서 매우 간단한 MVC 응용 프로그램을

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 "Model"클래스를 인스턴스화합니다.

기본 클래스 :

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 
    } 
} 

Google 주소록 클래스 : 나는 피드 항목을 반복하려고 할 때까지

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 - Unauthorized 오류가 발생합니다.

왜 이런 이유입니까? 문서를 Google Dev에 올리고 있습니다.

저는 1.7.0.1 버전의 API를 사용하고 있습니다.


참고 : 좀 다른 코드와 blog entry를 발견하고 그 어떤 작품, 같아요. 이제 반 공식적인 방식으로 작동하지 않는 이유를 알아보십시오! 아이디어가있는 사람은 누구입니까?

+0

전체 소스 코드 샘플을 사용하는 최종 해결책이 있습니까? – Kiquenet

답변

2

주위를 놀고 난 후에 나는 내가 필요한 것을 이루는 한 방법을 발견했다. 먼저 님이 오픈 아이디 인증 토큰을 사용하여 연락처, 캘린더 등을 볼 수있는 방법을 찾지 못했습니다. 그 방법을 계속 찾고 있습니다. (see my question here)

내가 알아 낸 것은 사람이 자신의 프로필에 Google 사용자 이름과 암호를 입력 한 다음 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; } 
    } 
} 

자신의 로그인 자격 증명을 요청 할 필요없이 자신의 오픈 ID 로그인을 대신 사용하는 방법을 알아 내기 위해, (! 그리고 그것은 오히려 중복)

다음

내가 생각 해낸 코드입니다!