javascriptgoogle-apigoogle-contacts-api

How to create a contact through people API in javascript?


How to pass the parent (i.e. Parameter) and Request Body so that it adds the contact to google contacts through People API.

function CreateContact() {
    gapi.client.people.people.createContact({
        parent: 'people/me',
        requestBody: {
            locales: [{value: 'en'}],
            genders: [{value: 'female'}]
        }
    })
}

Solution

  •  // 2. Initialize the JavaScript client library.
        gapi.client.init({
            'apiKey': 'Your API key',
            // clientId and scope are optional if auth is not required.
            'clientId': 'Your CLIENT ID',
            'scope': 'https://www.googleapis.com/auth/contacts',
        }).then(function () {
            // 3. Initialize and make the API request.
            return gapi.client.request({
                'method': "POST",
                'path': 'https://people.googleapis.com/v1/people:createContact',
                'datatype': 'jsonp',
                'parent': "Name the parent",
                'body': {
                    "names": [
                        {
                            "givenName": "Name to be given"
                        }
                    ],
                    "emailAddresses": [
                        {
                            "value": "Email_Add to be given"
                        }
                    ],
                    "phoneNumbers": [
                        {
                            "value": "phone number to be given"
                        }
                    ]
                }
            })
    
        }).then(function (response) {
            console.log(response.result);
            document.getElementById("test").innerHTML = "Create New contact Please Check into google contacts";
    
        }, function (reason) {
            console.log('Error: ' + reason.result.error.message);
        });
    };
    

    After Initializing the Javascript client library, you need to load it using:

    gapi.load('client', function_name_of_intitiation_method)