简体   繁体   English

用于获取联系人组列表的 Google People API C# 代码

[英]Google People API C# code to get list of contact groups

I'm using the new Google People API to load the list of a user's Google contacts into my app.我正在使用新的 Google People API 将用户的 Google 联系人列表加载到我的应用中。 Here's some of my class - as I said it's straight from the online guide and it works fine.这是我的一些课程 - 正如我所说,它直接来自在线指南,并且效果很好。 However I can't find any similar code to load the list of contact groups (eg family, workmates etc).但是我找不到任何类似的代码来加载联系人组列表(例如家人、同事等)。 I got this working before with the Contacts API and there is lots of sample code for that, but I can't find any for the People API.我之前使用 Contacts API 完成了这项工作,并且有很多示例代码,但我找不到 People API 的任何示例代码。 If this is still too broad a question please suggest how I can be more specific.如果这仍然是一个太宽泛的问题,请建议我如何更具体。 Thanks.谢谢。

using Google.Apis.Auth.OAuth2;
using Google.Apis.People.v1.Data;
using Google.Apis.People.v1;
using Google.Apis.Services; 

public class GoogleContacts
{
    private String m_client_secret = ".....";
    private String m_client_id = "......apps.googleusercontent.com";

    public GoogleContacts()
    {
        // Create OAuth credential.
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = m_client_id,
                ClientSecret = m_client_secret
            },
            new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly" },
            "me",
            CancellationToken.None).Result;

        // Create the service.
        var service = new PeopleService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "MyApp",
        });
        PeopleResource.ConnectionsResource.ListRequest peopleRequest =
            service.People.Connections.List("people/me");
        peopleRequest.RequestMaskIncludeField = new List<string>() {
        "person.addresses",
        "person.names"  };
        peopleRequest.SortOrder = (PeopleResource.ConnectionsResource.ListRequest.SortOrderEnum) 1;

       ListConnectionsResponse people = peopleRequest.Execute();

        if (people != null && people.Connections != null && people.Connections.Count > 0)
        {

            foreach (var person in people.Connections)
            {  //do stuff with people

// etc...

I eventually found a way to do it.我最终找到了一种方法来做到这一点。 Notice it's using PeopleService.v1 rather than People.v1.请注意,它使用的是 PeopleService.v1 而不是 People.v1。 Here's a class to show the list of groups then the list of contacts:这是一个显示组列表和联系人列表的类:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.PeopleService.v1;
using Google.Apis.PeopleService.v1.Data;

public class GoogleContacts
{

    private String m_client_secret = "......";
    private String m_client_id = ".......apps.googleusercontent.com";

    public GoogleContacts()
    {
        // Create OAuth credential.
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = m_client_id,
                ClientSecret = m_client_secret
            },
            new[] { "profile", "https://www.googleapis.com/auth/contacts.readonly" },
            "me",
            CancellationToken.None).Result;

        // Create the service.
        var service = new PeopleServiceService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "My App",
        });

        // Groups list ////////////
        ContactGroupsResource groupsResource = new ContactGroupsResource(service);
        ContactGroupsResource.ListRequest listRequest = groupsResource.List();
        ListContactGroupsResponse response = listRequest.Execute();

        // eg to show name of each group
        List<string> groupNames = new List<string>();
        foreach (ContactGroup group in response.ContactGroups)
        {
            groupNames.Add(group.FormattedName);
        }
        ///////////////

        // Contact list ////////////
        PeopleResource.ConnectionsResource.ListRequest peopleRequest =
            service.People.Connections.List("people/me");
        peopleRequest.PersonFields = "names,emailAddresses";
        peopleRequest.SortOrder = (PeopleResource.ConnectionsResource.ListRequest.SortOrderEnum) 1;
        ListConnectionsResponse people = peopleRequest.Execute();

        // eg to show display name of each contact
        List<string> contacts = new List<string>();
        foreach (var person in people.Connections)
        {
            contacts.Add(person.Names[0].DisplayName);

        }
        ///////////////
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM