【问题标题】:Google contacts are not showing by using Google People API使用 Google People API 未显示 Google 联系人
【发布时间】:2021-04-30 00:37:51
【问题描述】:

由于 Google 正在弃用 Google 联系人 API,而是建议我们使用 Google People API 来添加/创建/删除联系人。我能够创建、获取 Google 联系人,示例代码如下:

const { google } = require("googleapis")
const path = require("path")

const keyFile = path.join(__dirname, "serviceAccCredentials.json")
const scopes = [
  "https://www.googleapis.com/auth/contacts",
  "https://www.googleapis.com/auth/contacts.readonly"
]

function log(arg) {
  console.log(JSON.stringify(arg, null, 4))
}

const run = async () => {
  try {

    const { people, contactGroups } = google.people({
      version: "v1",
      auth: await google.auth.getClient({
        keyFile,
        scopes
      })
    })

    const createContact = await people.createContact(
        {
        requestBody: {
          names: [
            {
              givenName: "Yacov 3",
              familyName: "110$"
            }
          ],
          "memberships": [
            {
              "contactGroupMembership": {
                contactGroupId: 'myContacts'
                // "contactGroupResourceName": "contactGroups/myContacts"
              }
            }
          ]
        }
      }
    )
    log(createContact.data)

    const afterResponse = await people.connections.list({
      resourceName: "people/me",
      personFields: "names",
    })
    log(afterResponse.data)

  } catch (e) {
    console.log(e)
  }
}

run()

问题是我在 Google 联系人下看不到使用服务帐户创建的联系人。通常,服务帐户是为 G-suit 用户创建的,在 G-suit 域范围的委派设置下,我也添加了项目 ID 和范围。服务帐户中还启用了 People API。

此外,在Google's official documentation 的操场区,当我尝试创建 Google 联系人时,它起作用了。来自 API 资源管理器/游乐场的请求如下所示

     const createContact = await people.createContact({
        "personFields": "names",
        "sources": [
          "READ_SOURCE_TYPE_CONTACT"
        ],
        "prettyPrint": true,
        "alt": "json",
        "resource": {
          "names": [
            {
              "givenName": "test 2",
              "familyName": "playground"
            }
          ],
          "memberships": [
            {
              "contactGroupMembership": {
                "contactGroupResourceName": "contactGroups/myContacts"
              }
            }
          ]
        }
      })

奇怪的是,contactGroupResourceNamepersonFieldssourcesaltprettyPrint 等所有这些属性都不存在。

谁能真正告诉我发生了什么事。 PS:我不能也不想使用 OAuth2,因为应用程序将是服务器到服务器的通信,不会涉及任何人的同意。谢谢

【问题讨论】:

  • 问题是我没有看到使用服务帐户创建的联系人,您以谁的身份登录。如果它们是由服务帐户创建的,那么它们就是服务帐户联系人,您不会从自己的帐户中看到这一点,因为您没有访问权限。当您从服务帐户创建它们时,您是在委托域上的用户吗?
  • @DaImTo 有道理,When you create them from the service account are you deligating to a user on the domain ? 这是怎么做到的?你能解释一下吗

标签: google-api google-contacts-api google-people-api


【解决方案1】:

问题:

您可能为您的服务帐户启用了域范围的委派,但您没有使用它来模拟普通用户。

域范围委派的目的是让服务帐户代表域中的任何用户进行操作,但为了做到这一点,您必须指定您希望服务帐户模拟哪个用户。

否则,服务帐户将访问自己的资源(联系人、云端硬盘、日历等),而不是常规帐户的资源。因此,如果您使用常规帐户访问联系人 UI,您将看不到创建的联系人,因为联系人不是为此帐户创建的。

解决办法:

您需要模拟要为其创建联系人的帐户。

为了做到这一点,由于您使用的是 Node 的 getClient(),因此您应该指定要模拟的帐户的电子邮件地址,如 here 所示:

auth.subject = "email-address-to-impersonate";

更新:

在这种情况下,您可以执行以下操作:

let auth = await google.auth.getClient({
  keyFile,
  scopes
});
auth.subject = "email-address-to-impersonate";
const { people, contactGroups } = google.people({
  version: "v1",
  auth: auth
})

参考:

【讨论】:

  • TypeError: authClient.request is not a function 我在传递 auth.subject 后遇到错误,或者我在创建联系人中也尝试了 auth.email,例如 people.createContact({ auth: { email: 'ffbtest@fforb.com' }, requestBody: { names: [
  • @Danish 你在哪里添加这个?您正在为google.people 中的auth 参数构建值,那么您在哪里设置auth.subject="email"?我建议首先构建auth 参数,然后调用google.people({...})。我编辑了我的答案,以展示如何做到这一点。
  • 你好,是的,我在上面尝试了你的例子,现在我有一个不同的错误。 Error: unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested. at /home/xxx/people-api-project/node_modules/gtoken/build/src/index.js:272:35 at processTicksAndRejections (internal/process/task_queues.js:94:5)
  • @Danish 这可能意味着您在设置域范围的委派时没有向管理控制台添加适当的范围。按照指定的步骤 here 并确保添加了 contactscontacts.readonly(第 5 步)。
  • @Danish 不客气。很高兴您的问题得到了解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多