检索谷歌联系人

时间:2015-05-14 19:25:35

标签: c# api contact

我使用.net从我的Google联系人中检索联系信息。但是,它会检索所有包含未存储在“我的联系人”中的已发送电子邮件。无论如何,我只能从“我的联系人”中找回联系人吗?

这是代码

        RequestSettings rs = new RequestSettings("", email, password);
        rs.AutoPaging = true;
        ContactsRequest cr = new ContactsRequest(rs);

        Feed<Contact> Contacts = cr.GetContacts();

        foreach (Contact contact in Contacts.Entries)
        {

            Name name = contact.Name;
            Response.Write(name.GivenName + " " + name.FamilyName + "<br/>");
            foreach (EMail emailId in contact.Emails)
            {
                Response.Write(emailId.Address + "<br/>");
            }

            Response.Write("<br/>");
        }

1 个答案:

答案 0 :(得分:0)

我正在使用nuget v 2.2.0的Google.GData.Contacts包

string redirectUri = "urn:ietf:wg:oauth:2.0:oob";

// build the base oauth2 parameters
var Oauth2Params = new OAuth2Parameters
{
    ClientId = "your google app client id",
    ClientSecret = "your google app client secret",                
    RedirectUri = redirectUri
};

//security permissions to request from user
string scopes = "https://www.google.com/m8/feeds/ https://apps-apis.google.com/a/feeds/groups/";
Oauth2Params.Scope = scopes;

string url = OAuthUtil.CreateOAuth2AuthorizationUrl(Oauth2Params);
//start the default web browser
System.Diagnostics.Process.Start(url);
//then type your access code back to the console
Console.WriteLine("Please paste your Access code after authenticating via browser:");
Oauth2Params.AccessCode = Console.ReadLine();

OAuthUtil.GetAccessToken(Oauth2Params);
//store the access token securely somewhere 
//so you dont have to reauthenticate your app at every start

var reqFactory = new GOAuth2RequestFactory(Oauth2Params.Scope, "your google app name", Oauth2Params);

string queryUri = "https://www.google.com/m8/feeds/contacts/default/full";

ContactsQuery cq = new ContactsQuery(queryUri);
cq.NumberToRetrieve = 1000;
//cq.Group = ...group of your desire...
ContactsService contactService = new ContactsService("your google app name");
contactService.RequestFactory = reqFactory;
ContactsFeed feed = contactService.Query(cq);
//then foreach feed.Entries