Google People API与AppScript:将目录中的人员添加到一个群组中

huangapple go评论72阅读模式
英文:

Google People API with AppScript: Add People of Directory to a group

问题

我有一个公司/域中某人的邮件地址,并希望为他们分配一个Google联系人标签。

这是我的当前代码:

function main() {
  var labelName = "Test";
  var group = getContactGroup(labelName);
  var emailAddress = "john.smith@domain.com";
  var person = getPersonByEmailAdress(emailAddress);

  addPersonToGroup(person, group);
}

function getContactGroup(labelName) {
  const people = People.ContactGroups.list();
  const group = people['contactGroups'].find((group) => group['name'] === labelName);
  return group;
}

function getPersonByEmailAdress(emailAddress) {
  let request = People.People.searchDirectoryPeople({
    "query": emailAddress,
    "readMask": "emailAddresses",
    sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
  });
  return request.people[0];
}

function addPersonToGroup(person, group) {
  var personResourceName = person["resourceName"];
  var groupResourceName = group["resourceName"];

  var membersResource = {
    "resourceNamesToAdd": [
      personResourceName
    ]
  };
  People.ContactGroups.Members.modify(membersResource, groupResourceName); //在这里出错
}

对于最后一条语句,我收到以下错误消息:

GoogleJsonResponseException: API调用people.contactGroups.members.modify失败,错误消息为:资源名称"people/xxxxxxxxxxxxxxxxx"不是有效的联系人资源。

错误在哪里?我是否需要在此之前以某种方式将人添加到联系人中?而且,对于这么简单的问题,代码看起来有点复杂。是否有一种优化方法?

更大的背景是:我有一个通讯录联系人的邮件地址列表,想要将它们添加到一个组/分配相应的标签。只有列表中的人应该在组中,不在列表中的人应该从组中删除。

感谢您的帮助!

英文:

I have a mail adress of someone in my company/domain and want to assign them a google contact label.

This is my current code:

function main() {
  var labelName = "Test"
  var group = getContactGroup(labelName)
  var emailAddress = "john.smith@domain.com"
  var person = getPersonByEmailAdress(emailAddress)

  addPersonToGroup(person, group)
}

function getContactGroup(labelName) {
  const people = People.ContactGroups.list();
  const group = people['contactGroups'].find((group) => group['name'] === labelName);
  return group
}

function getPersonByEmailAdress(emailAddress) {
  let request = People.People.searchDirectoryPeople({
    "query": emailAddress,
    "readMask": "emailAddresses",
    sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
  });
  return request.people[0]
}

function addPersonToGroup(person, group) {
  var personResourceName = person["resourceName"];
  var groupResourceName = group["resourceName"];

  var membersResource = {
    "resourceNamesToAdd": [
      personResourceName
    ]
  }
  People.ContactGroups.Members.modify(membersResource, groupResourceName); //Error here
}

For the last statement I get the following error message:
> GoogleJsonResponseException: API call to people.contactGroups.members.modify failed with error: Resource name "people/xxxxxxxxxxxxxxxxx" is not a valid contact person resource.

Where is the mistake? Do I somehow need to add the person to contacts before? And also the code look a bot too complex for such an easy problem. Is there some way of optimizing it?

Bigger picture: I have a list of mail adresses of directory contacts and want to add them to a group/assign the respective label. Only people from the list should be in the group. people who are not in the list should be removed from the group.

Thanks for your help!

答案1

得分: 1

----UPDATE----

正如luisa指出的,问题可能与联系人实际不存在有关,可以通过getPersonByEmailAdress()函数添加联系人,如下所示:

function getPersonByEmailAdress() {
  let request = People.People.searchDirectoryPeople({
    "query": emailAddress,
    "readMask": "emailAddresses,names",
    sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
  });
  delete request.people[0]["metadata"]
  delete request.people[0]["resourceName"]
  delete request.people[0]["source"]
  delete request.people[0]["etag"]
  delete request.people[0].emailAddresses[0]["metadata"]
  delete request.people[0].names[0]["metadata"]

 People.People.createContact(request.people[0])

 return request.people[0]

}

之所以使用delete操作符,是因为request中存储的信息与创建新联系人存在冲突,例如资源名称(resourceName),它是一种不应该作为createContact请求的参数发送的唯一标识符。

----ORIGINAL ANSWER----

我通过修改以下代码中的行来使其工作:

 var personResourceName = person["resourceName"];
 var groupResourceName = group["resourceName"];

使用以下代码进行替换:

  var groupResourceName = ["resourceName"]
  var membersResource = {
    "resourceNamesToAdd": ["resourceName"]

似乎membersResource变量需要额外的数据才能被识别为资源名称。

为了得出这个结论,我在文档中使用了"尝试此方法"选项,并选择了扩展视图:

Google People API与AppScript:将目录中的人员添加到一个群组中

在那里,您可以选择"Javascript",如下所示:

Google People API与AppScript:将目录中的人员添加到一个群组中

一旦在那里,您可以在左侧填写"resourceName"(组资源)和"resourceNamesToAdd"(个人资源)的信息,这将在代码中添加一些行。从那里我们要找的函数是:


function execute() {
    return gapi.client.people.contactGroups.members.modify({
      "resourceName": "resourceName",
      "resource": {
        "resourceNamesToAdd": [
          "resourceName"
        ]
      }
    })
        .then(function(response) {
                // 处理结果在这里 (response.result包含解析后的内容)。
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }

您可以注意到,"resourceNamesToAdd"在引用个人资源名称时实际上包含更多信息。

希望这有所帮助!

英文:

----UPDATE----

As pointed out by luisa the issue might be related to the contact not actually existing, to fix this a contact can be added by the getPersonByEmailAdress() function like so:

function getPersonByEmailAdress() {
  let request = People.People.searchDirectoryPeople({
    "query": emailAddress,
    "readMask": "emailAddresses,names",
    sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
  });
  delete request.people[0]["metadata"]
  delete request.people[0]["resourceName"]
  delete request.people[0]["source"]
  delete request.people[0]["etag"]
  delete request.people[0].emailAddresses[0]["metadata"]
  delete request.people[0].names[0]["metadata"]

 People.People.createContact(request.people[0])

 return request.people[0]

}

The delete operator is in there because the information stored in request contains data that conflicts with the creation of a new contact such as a resourceName which is sort of a unique ID that should never be sent as a parameter for a createContact request.

----ORIGINAL ANSWER----
I was able to make this work by modifying the lines in

 var personResourceName = person["resourceName"];
 var groupResourceName = group["resourceName"];

with

  var groupResourceName = ["resourceName"]
  var membersResource = {
    "resourceNamesToAdd": ["resourceName"]

it seems like the membersResource variable requires an extra bit of data to be recognized as a resource name.

To reach that conclussion I used the try this method option in the documentation and selected the option to expand the view:

Google People API与AppScript:将目录中的人员添加到一个群组中

There you can select "Javascript" as such:

Google People API与AppScript:将目录中的人员添加到一个群组中

Once there you can fill the information to the left where the "resourceName" (group resource) and the "resourceNamesToAdd" (person resource) can be filled in, doing so will add some lines to the code. From there the function we are looking for is:


function execute() {
    return gapi.client.people.contactGroups.members.modify({
      "resourceName": "resourceName",
      "resource": {
        "resourceNamesToAdd": [
          "resourceName"
        ]
      }
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }

Where you can notice how "resourceNamesToAdd" actually has more information when referring to the person resource name.

Hope this helps!

huangapple
  • 本文由 发表于 2023年7月18日 01:04:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定