To use the library, you must first load the JavaScript client library via gapi.load('client', callback) and then initialize it using gapi.client.init().
Initialization requires an apiKey. If you need to access private user data, you must also provide a clientId and scope. To use specific Google APIs (like People or Drive) with the discovery-based method, you must provide their corresponding Discovery Document URLs in the discoveryDocs array.
// 1. Load the JavaScript client library.
gapi.load('client', function() {
// 2. Initialize the JavaScript client library.
gapi.client.init({
'apiKey': 'YOUR_API_KEY',
'discoveryDocs': ['https://people.googleapis.com/$discovery/rest'],
'clientId': 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
'scope': 'profile',
}).then(function() {
// 3. Make the API request.
return gapi.client.people.people.get({
'resourceName': 'people/me',
'requestMask.includeField': 'person.names'
});
}).then(function(response) {
console.log(response.result);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
});