首页 > 解决方案 > 如何使用 Google 联系人 api v3 创建新联系人 - 使用 javascript

问题描述

即使这个问题在过去一年中已经被多次提出,它还没有得到正确解决……尤其是在使用最新的 googleapis lib 时!谷歌文档没有用,因为仍然提到了许多不推荐使用的方法,并且没有给出 javascript 示例......

使用 people.get() (在 list() 之后)我可以了解它应该如何(我猜)

    {
    "resourceName":"people/c3451490284670017263",
    "etag":"%EgcBAggJNyUuGgwBAgMEBQYHCAkKCwwiDHVtR0Y5OHZVMnhJPQ==",
    "locales":[{"metadata":{"primary":true,"source":{"type":"CONTACT","id":"2fe628208b77deef"}},"value":"en"}],
    "emailAddresses":[{"metadata":{"primary":true,"source":{"type":"CONTACT","id":"2fe628208b77deef"}},"value":"john.doe@example.com"]
    }

所以我尝试以这种方式创建一个新人:

    return google.people('v1').people.createContact({
      auth: jwtClient,
      resourceName: 'people/me',
      locales: ['en'],
      genders: ['female'],
      names: [{givenName: 'Jenny', familyName: 'Doe'}],
      emailAddresses: ['jenny.doe@example.com']
    })

但没办法......我总是遇到错误:

        Invalid JSON payload received. Unknown name \"genders\": Cannot bind query parameter. Field 'genders' could not be found in request message.
        Invalid JSON payload received. Unknown name \"locales\": Cannot bind query parameter. Field 'locales' could not be found in request message.
        Invalid JSON payload received. Unknown name \"names[familyName]\": Cannot bind query parameter. Field 'names[familyName]' could not be found in request message.
        Invalid JSON payload received. Unknown name \"emailAddresses\": Cannot bind query parameter. Field 'emailAddresses' could not be found in request message.
        Invalid JSON payload received. Unknown name \"names[givenName]\": Cannot bind query parameter. Field 'names[givenName]' could not be found in request message.
        Invalid JSON payload received. Unknown name \"resourceName\":Cannot bind query parameter. Field 'resourceName' could not be found in request message.

网络上是否存在任何示例?

欢迎反馈......(甚至来自谷歌人......哈哈)

标签: javascriptgoogle-apigoogle-people-api

解决方案


在仔细检查(可能是三重......)文档后,我阅读了以下声明

    Usage
    Specifying request body
    The body of the request is specified in the requestBody parameter object of the request. The body is specified as a JavaScript object with key/value pairs. For example, this sample creates a watcher that posts notifications to a Google Cloud Pub/Sub topic when emails are sent to a gmail account:

    const res = await gmail.users.watch({
      userId: 'me',
      requestBody: {
        // Replace with `projects/${PROJECT_ID}/topics/${TOPIC_NAME}`
        topicName: `projects/el-gato/topics/gmail`
      }
    });
    console.log(res.data);

所以我写道:

    return google.people('v1').people.createContact({
      auth: jwtClient,
      parent: 'people/me',
      requestBody: {
        locales: [{ value: 'en' }],
        genders: [{ value: 'female' }],
        names: [{ givenName: 'Jenny', familyName: 'Doe' }],
        emailAddresses: [{ value: 'jenny.doe@example.com' }]            
      }
    })

并且请求执行没有任何错误......我可以在 mt Google 帐户应用程序联系人中看到新联系人..


推荐阅读