简体   繁体   中英

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

Even if this issue has been raised many times over the past year, it's not yet correctly solved ... especially when using the latest googleapis lib ! Google doc is not useful as many deprecated methods are still mentioned and no javascript examples are given...

using people.get() ( after a list() ) I can get a view on how it should be ( I guess)

    {
    "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"]
    }

so I tried to create a new People this way :

    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']
    })

but NO WAY... I am always getting errors :

        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.

Is any example existing on the web ?

feedback welcome ... ( even from a Google man... LOL )

After double-checking (maybe triple...) the documentation , I read the following statement

    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);

so I wrote :

    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' }]            
      }
    })

And the request is executed without any error ... I can see the new contact in mt Google account app Contacts..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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