【问题标题】:Get name and email of any google directory user via service account通过服务帐户获取任何 google 目录用户的姓名和电子邮件
【发布时间】:2021-01-21 17:51:05
【问题描述】:

我有一个聊天机器人,它使用 Google 服务帐户连接到 Google People API。机器人收到一个包含消息发件人的 Google ID(即1234567890987654321)的事件。我想使用服务帐户查找邮件发件人的姓名和电子邮件地址。

我相信 https://www.googleapis.com/auth/directory.readonly scope should allow this 为服务帐户设置了域范围的委派。但响应不包含请求的字段,仅填充了 Etag 和 ResourceName。

我可以更改或配置什么以在使用服务帐户的People.Get 呼叫中包含任意目录用户的姓名和电子邮件?

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

Go Playground

【问题讨论】:

  • 我无法尝试这个(没有域)但是,请参阅here JWT.Subject 设置为所需用户的电子邮件帐户并获得了一个令牌,这就是实例化时用于身份验证的内容服务。是的,Admin SDK 不是人,而是……
  • 这有点同意 People API auth doc。我假设 namesemailAddresses 是私有而非公共数据,因此您必须使用 OAuth2 令牌进行身份验证。
  • 每当我阅读 OAuth2 时,我都会认为“需要在浏览器中提示用户登录”。
  • 如果您的问题已解决,您能否在此处发布答案以供文档之用?
  • @Chris 我添加了问题的答案。希望对您有所帮助!

标签: go google-people-api hangouts-chat hangouts-api


【解决方案1】:

事实证明,谷歌聊天在 json 响应中返回了电子邮件地址,它只是在 chat.DeprecatedEvent 中丢失了。我创建了新结构来捕获其他数据,完全避免使用 person API。

package lib

// This package creates a customized chat.DeprecatedEvent from v0.37.0 of the google chat api
import chat "google.golang.org/api/chat/v1"

// ChatEvent Google Chat event with customized User object
// Both ChatEvent.User and ChatEvent.Message.Sender should have the same fields,
// but only ChatEvent.User is modified to use ChatUser for simplicity
type ChatEvent struct {
    // Action: The form action data associated with an interactive card that
    // was clicked. Only populated for CARD_CLICKED events. See the
    // Interactive Cards guide (/hangouts/chat/how-tos/cards-onclick) for
    // more information.
    Action *chat.FormAction `json:"action,omitempty"`

    // ConfigCompleteRedirectUrl: The URL the bot should redirect the user
    // to after they have completed an authorization or configuration flow
    // outside of Google Chat. See the Authorizing access to 3p services
    // guide (/hangouts/chat/how-tos/auth-3p) for more information.
    ConfigCompleteRedirectURL string `json:"configCompleteRedirectUrl,omitempty"`

    // EventTime: The timestamp indicating when the event was dispatched.
    EventTime string `json:"eventTime,omitempty"`

    // Message: The message that triggered the event, if applicable.
    Message *chat.Message `json:"message,omitempty"`

    // Space: The room or DM in which the event occurred.
    Space *chat.Space `json:"space,omitempty"`

    // ThreadKey: The bot-defined key for the thread related to the event.
    // See the thread_key field of the `spaces.message.create` request for
    // more information.
    ThreadKey string `json:"threadKey,omitempty"`

    // Token: A secret value that bots can use to verify if a request is
    // from Google. The token is randomly generated by Google, remains
    // static, and can be obtained from the Google Chat API configuration
    // page in the Cloud Console. Developers can revoke/regenerate it if
    // needed from the same page.
    Token string `json:"token,omitempty"`

    // Type: The type of the event.
    //
    // Possible values:
    //   "UNSPECIFIED" - Default value for the enum. DO NOT USE.
    //   "MESSAGE" - A message was sent in a room or direct message.
    //   "ADDED_TO_SPACE" - The bot was added to a room or DM.
    //   "REMOVED_FROM_SPACE" - The bot was removed from a room or DM.
    //   "CARD_CLICKED" - The bot's interactive card was clicked.
    Type string `json:"type,omitempty"`

    // User: The customized hangouts chat user that triggered the event.
    User *ChatUser `json:"user,omitempty"`
}

// ChatUser A custom hangouts chat user that contains the fields currently sent from google as of 26-Jan-2021
type ChatUser struct {
    // Name: Google's name for the user, such as users/1234567890987654321
    Name string `json:"name"`
    // DisplayName: The first and last name of the user, such as John Doe
    DisplayName string `json:"displayName"`
    // AvatarURL: full URL to an avatar image
    AvatarURL string `json:"avatarUrl"`
    // Email: standard email address
    Email string `json:"email"`
    // Type: see chat.User.Type, typically this will be HUMAN
    Type string `json:"type"`
    // DomainID: see chat.User.DomainId
    DomainID string `json:"domainId"`
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2016-06-08
    相关资源
    最近更新 更多