Google 联系人未通过使用 Google 人 API 显示
Google contacts are not showing by using Google People API
由于 Google 弃用 Google 联系人 API,而是建议我们使用 Google 人 API 到 add/create/delete 联系人。我能够创建、获取 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。此外,在服务帐户中启用了人员 API。
此外,在 Google's official documentation 的操场区域,当我尝试创建一个 Google 联系人时,它成功了。来自 API explorer / playground 的请求看起来像这样
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,因为该应用程序将成为服务器到服务器的通信,不会涉及任何人的同意。谢谢
问题:
您可能已经为您的服务帐户启用了全域委派,但您并未使用它来模拟普通用户。
全域委派的目的是让服务帐户代表域中的任何用户,但为了做到这一点,您必须指定您想要服务的用户要模拟的帐户.
否则,服务帐户将访问它自己的资源(它的联系人、它的驱动器、它的日历等),而不是普通帐户的资源。因此,如果您使用常规帐户访问联系人 UI,您将看不到创建的联系人,因为没有为此帐户创建联系人。
解决方案:
您需要模拟您要为其创建联系人的帐户。
为了做到这一点,因为您使用的是 Node 的 getClient(), you should specify the email address of the account you want to impersonate, as shown 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
})
参考:
由于 Google 弃用 Google 联系人 API,而是建议我们使用 Google 人 API 到 add/create/delete 联系人。我能够创建、获取 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。此外,在服务帐户中启用了人员 API。
此外,在 Google's official documentation 的操场区域,当我尝试创建一个 Google 联系人时,它成功了。来自 API explorer / playground 的请求看起来像这样
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,因为该应用程序将成为服务器到服务器的通信,不会涉及任何人的同意。谢谢
问题:
您可能已经为您的服务帐户启用了全域委派,但您并未使用它来模拟普通用户。
全域委派的目的是让服务帐户代表域中的任何用户,但为了做到这一点,您必须指定您想要服务的用户要模拟的帐户.
否则,服务帐户将访问它自己的资源(它的联系人、它的驱动器、它的日历等),而不是普通帐户的资源。因此,如果您使用常规帐户访问联系人 UI,您将看不到创建的联系人,因为没有为此帐户创建联系人。
解决方案:
您需要模拟您要为其创建联系人的帐户。
为了做到这一点,因为您使用的是 Node 的 getClient(), you should specify the email address of the account you want to impersonate, as shown 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
})