fedesc
fedesc

Reputation: 2610

Google PeopleApi - CreateContact method invalid argument

I'm trying to create a contact from my application and documentation is hideous!

i build a contact object from data i receive by form

var contact = {
  names: [
    {
      displayName: body.name
    }
  ],
  phoneNumbers: [
    {
      value: body.phone
    }
  ],
  biographies: [
    {
      value: body.notes
    }
  ],
  userDefined: [
  {
      value: body.snif
    },
    {
      value: body.position
    },
    {
      value: body.sms
    },
    {
      value: body.whatsapp
    }
  ]
};

and try to make the request but i can't figure out what's wrong

const service = google.people({version: 'v1', auth});
service.people.createContact({
    parent: 'people/me', 
    resource: contact
}, {}, function(err, res) {
    console.log(err)
});

and i get 400 INVALID_ARGUMENTS

errors:
[ { message: 'Request contains an invalid argument.',
   domain: 'global',
   reason: 'badRequest' } ] };

Nodejs, ExpressJs

please what am i doing wrong?

Upvotes: 1

Views: 411

Answers (1)

Tanaike
Tanaike

Reputation: 201378

How about this modification?

Modification points:

  • The reason of error is to be not keys for values. For property of userDefined, please add a property of key.
    • By this, you can retrieve the value using the key.

Modified script:

Please modify userDefined as follows. These are sample keys. So please modify them for your situation.

userDefined: [
  {
    value: body.snif,
    key: "snif", // Added
  },
  {
    value: body.position,
    key: "position", // Added
  },
  {
    value: body.sms,
    key: "sms", // Added
  },
  {
    value: body.whatsapp,
    key: "whatsapp", // Added
  },
],

Also if you want to retrieve the response from API, how about modifying as follows?

service.people.createContact({
  parent: 'people/me',
  resource: contact,
}, {}, function(err, res) {
    if (err) {
      console.log(err.errors);
      return;
    }
    console.log(res.data);
});

Note:

  • This modified script supposes that you can use the API and scopes for creating contacts.

References:

If I misunderstand your question, please tell me. I would like to modify it.

Upvotes: 2

Related Questions