繁体   English   中英

如何使用Google Contacts API v3创建新联系人-使用JavaScript

[英]How to create a new contact with Google contacts api v3 - using javascript

即使在过去一年中多次提出此问题,但仍无法正确解决...尤其是在使用最新的googleapis lib时! Google文档没有用,因为仍然提到了许多不推荐使用的方法,并且没有给出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.

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

欢迎反馈...(甚至来自Google员工...哈哈)

在仔细检查(可能是三遍...)文档之后,我阅读了以下语句

    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帐户应用“通讯录”中看到新的联系人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM