Accessing the People API from C# / .NET
The Google People API has launched, this post describes how to access the API from .NET projects using C#.
Code overview
The following code shows how to access the API:
using Google.Apis.Auth.OAuth2; using Google.Apis.People.v1; using Google.Apis.People.v1.Data; using Google.Apis.Services; using Google.Apis.Util.Store; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace PeopleQuickstart { class Program { // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/people-dotnet-quickstart.json static string[] Scopes = { PeopleService.Scope.ContactsReadonly }; static string ApplicationName = "People API .NET Quickstart"; static ClientSecrets secrets = new ClientSecrets() { ClientId = "YOUR_CLIENT_ID", ClientSecret = "YOUR_CLIENT_SERCRET" }; static void Main(string[] args) { UserCredential credential; string credPath = System.Environment.GetFolderPath( System.Environment.SpecialFolder.Personal); credPath = Path.Combine(credPath, ".credentials/people-dotnet-quickstart"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync( secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); // Create Drive API service. var service = new PeopleService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); // List People. Console.WriteLine("People:"); GetPeople(service, null); Console.WriteLine("Done!"); Console.Read(); } static void GetPeople(PeopleService service, string pageToken) { // Define parameters of request. PeopleResource.ConnectionsResource.ListRequest peopleRequest = service.People.Connections.List("people/me"); if (pageToken != null) { peopleRequest.PageToken = pageToken; } ListConnectionsResponse people = peopleRequest.Execute(); if (people != null && people.Connections != null && people.Connections.Count > 0) { foreach (var person in people.Connections) { Console.WriteLine(person.Names != null ? (person.Names[0].DisplayName ?? "n/a") : "n/a"); } if (people.NextPageToken != null) { GetPeople(service, people.NextPageToken); } } else { Console.WriteLine("No people found / end of list"); return; } } } }
Quickstart
1. Create a project in Visual Studio and install the People API NuGet package.
2. Add a new project on the Google Developer Console. Create a project, add the People API, and add an API key of type Other.
3. From your project Credentials, copy the client ID and secret into your Visual Studio project, replacing YOUR_CLIENT_ID and YOUR_CLIENT_SECRET in the provided code.
4. Build and run, after you authenticate, you will see the contacts available to the app.
you get Error tye type name connections reserouce does not esit int he type
Make sure to enable the API in the API console and configure your app with a client ID and client secret.
Receiving an error “personFields mask is required”.
This code no longer seems to work without adding a mask.
I was able to make it work by adding this line before the first request happens…
peopleRequest.RequestMaskIncludeField = “person.names”;
According to https://developers.google.com/people/api/rest/v1/people/get I’m still using a deprecated field, but at least it’s working. Thanks for getting me started..