Reputation: 147
I'm using the new Google People API to load the list of a user's Google contacts into my app. 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. 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...
Upvotes: 0
Views: 2888
Reputation: 147
I eventually found a way to do it. Notice it's using PeopleService.v1 rather than 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);
}
///////////////
}
}
Upvotes: 4