【问题标题】:Google People API C# code to get list of contact groups用于获取联系人组列表的 Google People API C# 代码
【发布时间】:2019-02-22 15:15:02
【问题描述】:

我正在使用新的 Google People API 将用户的 Google 联系人列表加载到我的应用中。这是我的一些课程——正如我所说,它直接来自在线指南,而且效果很好。但是我找不到任何类似的代码来加载联系人组列表(例如家庭、同事等)。我之前使用 Contacts API 进行过此操作,并且有很多示例代码,但我找不到 People API 的任何示例代码。 如果这仍然是一个太宽泛的问题,请建议我如何更具体。谢谢。

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

【问题讨论】:

标签: c# google-people-api


【解决方案1】:

我最终找到了一种方法。请注意,它使用的是 PeopleService.v1 而不是 People.v1。这是一个显示组列表然后显示联系人列表的类:

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);

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

【讨论】:

  • 非常感谢您阐明 PeopleService.v1 和 PeopleServiceService 对象。
  • 但是问题,它的列表只有 100 个联系人。为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多