问题
I'm trying to pull down a list of a persons Google Mail contacts in MVC 3 using this API: http://code.google.com/apis/contacts/ http://code.google.com/apis/contacts/docs/3.0/developers_guide_dotnet.html#Retrieving
I need help with where to start when implementing it. I've read through the documentation numerous times, but it's just not making sense to me.
I want to pull down all the contacts and display them in the Index action of my Contacts Controller.
Any help with this broad question would be greatly appreciated.
回答1:
First get some sample code working from the above (EDIT - see below actually)
Then create a new ViewModel with whatever fields in it you want to display.
Create a strongly typed view for this ViewModel as a List of ContactsViewModel (whatever you call it) such that at the top of your view its:
@model IEnumerable<ContactsViewModel>
Create a route to point a URL to your controller's method, then in your method simply query from google, and populate using a loop, LINQ, (or automapper, etc) your ContactsViewModel, and return the model to the view.
If you have something more specific on top of that let me know and I'll go deeper
There are some improperly named /formatted items in their same code ironically : ) of course add the references below (contacts, client, extensions) from the C:\Program Files\Google\Google Data API SDK\Redist folder
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", "youraccount@gmail.com", "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);
}
}
}
}
}
来源:https://stackoverflow.com/questions/5860749/mvc-3-and-google-contacts-api