如何通过javascript中的people API创建联系人?

时间:2018-07-05 03:58:43

标签: javascript google-api google-contacts

如何传递父级(即参数)和请求正文,以便通过People API将联系人添加到Google联系人中。

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

1 个答案:

答案 0 :(得分:0)

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

初始化Javascript客户端库后,您需要使用以下命令加载它:

gapi.load('client', function_name_of_intitiation_method)