2011-05-02 92 views
0

我試圖拉下的MVC 3一個人的谷歌郵件聯繫人使用此API列表:http://code.google.com/apis/contacts/ http://code.google.com/apis/contacts/docs/3.0/developers_guide_dotnet.html#RetrievingMVC 3和谷歌聯繫人API

我需要在哪裏執行,當它開始幫助。我已經多次閱讀過這些文檔,但這對我來說沒有意義。

我想拉下所有聯繫人並將其顯示在我的聯繫人控制器的索引操作中。

任何幫助這個廣泛的問題將不勝感激。

回答

3

首先從上面得到一些示例代碼(編輯 - 實際上見下面) 然後創建一個新的ViewModel,其中包含您想要顯示的任何字段。 創建此視圖模型一個強類型視圖的ContactsViewModel列表(無論你怎麼稱呼它),這樣,在您的視圖頂部的: @model IEnumerable<ContactsViewModel>

創建一個URL指向您的控制器的方法的路線,然後在你的方法只需從谷歌查詢,並使用循環,LINQ(或自動映射等)填充你的ContactsViewModel,然後將模型返回到視圖。

如果你有什麼最重要的是更具體的,讓我知道,我會更深入

有其相同的代碼不正確的一些命名/格式化項目具有諷刺意味的:)當然 下面添加引用(從C聯繫人,客戶,擴展):\ Program Files文件\谷歌\谷歌數據API SDK \的Redist文件夾

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


namespace GoogleTests 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      RequestSettings rs = new RequestSettings("myApplication", "[email protected]", "yourpwd"); 
      // AutoPaging results in automatic paging in order to retrieve all contacts 

      rs.AutoPaging = true; 
      ContactsRequest cr = new ContactsRequest(rs); 

      Feed<Contact> f = cr.GetContacts(); 
      foreach (Contact entry in f.Entries) 
      { 
       if (entry.Name != null) 
       { 
        Name name = entry.Name; 
        if (!string.IsNullOrEmpty(name.FullName)) 
         Console.WriteLine("\t\t" + name.FullName); 
        else 
         Console.WriteLine("\t\t (no full name found)"); 
        if (!string.IsNullOrEmpty(name.NamePrefix)) 
         Console.WriteLine("\t\t" + name.NamePrefix); 
        else 
         Console.WriteLine("\t\t (no name prefix found)"); 
        if (!string.IsNullOrEmpty(name.GivenName)) 
        { 
         string givenNameToDisplay = name.GivenName; 
         if (!string.IsNullOrEmpty(name.GivenNamePhonetics)) 
          givenNameToDisplay += " (" + name.GivenNamePhonetics + ")"; 
         Console.WriteLine("\t\t" + givenNameToDisplay); 
        } 
        else 
         Console.WriteLine("\t\t (no given name found)"); 
        if (!string.IsNullOrEmpty(name.AdditonalName)) 
        { 
         string additionalNameToDisplay = name.AdditonalName; 
         if (string.IsNullOrEmpty(name.AdditionalNamePhonetics)) 
          additionalNameToDisplay += " (" + name.AdditionalNamePhonetics 
         + ")"; 
         Console.WriteLine("\t\t" + additionalNameToDisplay); 
        } 
        else 
         Console.WriteLine("\t\t (no additional name found)"); 
        if (!string.IsNullOrEmpty(name.FamilyName)) 
        { 
         string familyNameToDisplay = name.FamilyName; 
         if (!string.IsNullOrEmpty(name.FamilyNamePhonetics)) 
          familyNameToDisplay += " (" + name.FamilyNamePhonetics + ")"; 
         Console.WriteLine("\t\t" + familyNameToDisplay); 
        } 
        else 
         Console.WriteLine("\t\t (no family name found)"); 
        if (!string.IsNullOrEmpty(name.NameSuffix)) 
         Console.WriteLine("\t\t" + name.NameSuffix); 
        else 
         Console.WriteLine("\t\t (no name suffix found)"); 
       } 
       else 
        Console.WriteLine("\t (no name found)"); 

       foreach (EMail email in entry.Emails) 
       { 
        Console.WriteLine("\t" + email.Address); 
       } 
      } 
     } 
    } 
} 
+0

問題越來越一些示例代碼的工作,我知道當我得到一些樣本做什麼代碼去。我只是不知道如何拼湊在一起。 – dalehumphries 2011-05-02 18:35:15

+1

好的 - 我會在這裏一步一步地簡單地運行api – 2011-05-02 18:46:09

+0

好的,我已經在助手文件中看到了代碼,它看起來都很好。現在我如何將這些聯繫人加入我的數據庫? – dalehumphries 2011-05-02 19:14:36