Greg
Greg

Reputation: 1024

Google People API: null when listing connections

When using Google.Apis.PeopleService.v1, the people.connections.list method seems to work (no exceptions are thrown) but the returned object always have all its fields set to null. Here is the relevant code:

var scopes = new[] { PeopleServiceService.Scope.ContactsReadonly, PeopleServiceService.ScopeConstants.Contacts };
var credential = GoogleCredential.FromFile("./my-secrets.json")
                                 .CreateScoped(scopes);
var service = new PeopleServiceService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
});

var req = service.People.Connections.List("people/me");
req.PageSize = 2000;
req.RequestMaskIncludeField = "person.names";
req.Fields = "connections,totalItems,nextSyncToken";

var res = await req.ExecuteAsync();
Console.WriteLine(res.Connections.Count); // <-- NullReferenceException

my-secrets.json is the secrets file downloaded from console.developers.google.com. It looks like this:

{
  "type": "service_account",
  "project_id": "",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
}

Does someone know what I'm missing ?

Upvotes: 1

Views: 323

Answers (1)

Chris
Chris

Reputation: 1705

The PeopleService API requires a user credential, not a service credential.

In a locally installed app (e.g. a console app), use GoogleWebAuthorizationBroker.AuthorizeAsync(...) to get one of these.

The request must set req.PersonFields as described here: https://developers.google.com/people/api/rest/v1/people.connections/list#query-parameters E.g. req.PersonFields = "emailAddresses";

It is not necessary to set req.Fields or req.RequestMaskIncludeField.

Upvotes: 0

Related Questions