Google联系人不显示使用Google People API



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

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

问题是,我没有看到与谷歌联系人下的服务帐户创建的联系人。通常,服务帐户是为G-suit用户创建的,在G-suit域范围授权设置下,我还添加了具有范围的项目id。另外,在服务帐户中启用了People API。

此外,在谷歌官方文档的游乐场区域,当我试图创建一个谷歌联系人时,它工作了。来自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"
}
}
]
}
})

奇怪的是,contactGroupResourceName,personFields,sources,alt,prettyPrint这些性质都不存在。

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

Issue:

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

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

否则,服务帐户将访问其自己的资源(联系人、驱动器、日历等),而不是普通帐户的资源。因此,如果您使用常规帐户访问联系人UI,则不会看到创建的联系人,因为没有为该帐户创建联系人。

解决方案:

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

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

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
})
参考:

  • Google Auth Library: Node.js Client

最新更新