【发布时间】: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"
}
}
]
}
})
奇怪的是,contactGroupResourceName、personFields、sources、alt、prettyPrint 等所有这些属性都不存在。
谁能真正告诉我发生了什么事。 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