I have a chat bot that connects to the Google People API with a google service account. The bot receives an event that contains the message sender's Google ID (ie 1234567890987654321). I would like to look up the message sender's name and email address using the service account.
I believe that the https://www.googleapis.com/auth/directory.readonly scope should allow this, with domain-wide delegation set for the service account. But the response does not include the requested fields, only Etag and ResourceName are populated.
What can I change or configure to include the name and email of an arbitrary directory user in a People.Get call with a service account?
package main
import (
"context"
"log"
"google.golang.org/api/option"
"google.golang.org/api/people/v1"
)
func main() {
// Service account's credentials
apiKeyFile := "credentials.json"
// Google ID of a person within your directory
resourceName := "people/1234567890987654321"
fields := "names,emailAddresses"
ctx := context.Background()
// directory.readonly scope is included by default
s, _ := people.NewService(ctx, option.WithCredentialsFile(apiKeyFile))
pCall := s.People.Get(resourceName)
pCall.PersonFields(fields)
person, _ := pCall.Do()
log.Print(person.Etag)
for _, address := range person.EmailAddresses {
log.Print(address.Value)
}
for _, name := range person.Names {
log.Print(name.DisplayName)
}
}
It turns out that Google Chat is returning the email address in the json response, it is just missing from
chat.DeprecatedEvent. I created new structs to capture the other data, avoiding the person API altogether.