title: How to create google contacts using People API and Nodejs
body: <p>Im searching for how to create google contacts using the <code>People API</code>, i have followed the guide here:</p>
  <p><a href="https://developers.google.com/people/quickstart/nodejs" rel="nofollow noreferrer">https://developers.google.com/people/quickstart/nodejs</a></p>
  <p>My current code:</p>
  <pre class="lang-js prettyprint-override"><code>const fs = require('fs');
  const readline = require('readline');
  const {google} = require('googleapis');
  
  // If modifying these scopes, delete token.json.
  const SCOPES = ['https://www.googleapis.com/auth/contacts'];
  // The file token.json stores the user's access and refresh tokens, and is
  // created automatically when the authorization flow completes for the first
  // time.
  const TOKEN_PATH = 'token.json';
  
  // Load client secrets from a local file.
  fs.readFile('credentials.json', (err, content) =&gt; {
    if (err) return console.log('Error loading client secret file:', err);
    // Authorize a client with credentials, then call the Google Tasks API.
    authorize(JSON.parse(content), listConnectionNames);
  });
  
  /**
   * Create an OAuth2 client with the given credentials, and then execute the
   * given callback function.
   * @param {Object} credentials The authorization client credentials.
   * @param {function} callback The callback to call with the authorized client.
   */
  function authorize(credentials, callback) {
    const {client_secret, client_id, redirect_uris} = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(
        client_id, client_secret, redirect_uris[0]);
  
    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, (err, token) =&gt; {
      if (err) return getNewToken(oAuth2Client, callback);
      oAuth2Client.setCredentials(JSON.parse(token));
      callback(oAuth2Client);
    });
  }
  
  /**
   * Get and store new token after prompting for user authorization, and then
   * execute the given callback with the authorized OAuth2 client.
   * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
   * @param {getEventsCallback} callback The callback for the authorized client.
   */
  function getNewToken(oAuth2Client, callback) {
    const authUrl = oAuth2Client.generateAuthUrl({
      access_type: 'offline',
      scope: SCOPES,
    });
    console.log('Authorize this app by visiting this url:', authUrl);
    const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout,
    });
    rl.question('Enter the code from that page here: ', (code) =&gt; {
      rl.close();
      oAuth2Client.getToken(code, (err, token) =&gt; {
        if (err) return console.error('Error retrieving access token', err);
        oAuth2Client.setCredentials(token);
        // Store the token to disk for later program executions
        fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) =&gt; {
          if (err) return console.error(err);
          console.log('Token stored to', TOKEN_PATH);
        });
        callback(oAuth2Client);
      });
    });
  }
  
  /**
   * Print the display name if available for 10 connections.
   *
   * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
   */
  function listConnectionNames(auth) {
    const service = google.people({version: 'v1', auth});
    service.people.connections.list({
      resourceName: 'people/me',
      pageSize: 2,
      personFields: 'names,emailAddresses',
    }, (err, res) =&gt; {
      if (err) return console.error('The API returned an error: ' + err);
      const connections = res.data.connections;
      if (connections) {
        console.log('Connections:');
        connections.forEach((person) =&gt; {
          if (person.names &amp;&amp; person.names.length &gt; 0) {
            console.log(person.names[0].displayName);
          } else {
            console.log('No display name found for connection.');
          }
        });
      } else {
        console.log('No connections found.');
      }
    });
  
    service.people.createContact({
      requestBody: {
        emailAddresses: [{value: 'test@test.com'}],
        names: [
          {
            displayName: 'A',
            familyName: 'B',
            givenName: 'C',
          },
        ],
      },
    });
    //console.log('\n\nCreated Contact:', newContact);
  
  }
  </code></pre>
  <p>I succeed in <code>reading</code> my contacts and in creating the OAuth authentication key.</p>
  <p><a href="https://developers.google.com/people/api/rest/v1/people/createContact" rel="nofollow noreferrer">https://developers.google.com/people/api/rest/v1/people/createContact</a>
  (Sadly this link doesn't have a full working example like on the link to read contacts)</p>
  <p>I tried to create a contact adding this code:</p>
  <pre class="lang-js prettyprint-override"><code>  service.people.createContact({
      requestBody: {
        emailAddresses: [{value: 'test@test.com'}],
        names: [
          {
            displayName: 'A',
            familyName: 'B',
            givenName: 'C',
          },
        ],
      },
    });
  </code></pre>
  <p>But, when i add the code, it return an error:</p>
  <pre><code>(node:8392) UnhandledPromiseRejectionWarning: Error: Insufficient Permission
      at Gaxios.&lt;anonymous&gt; (C:\Users\Desktop\Nova pasta\node_modules\gaxios\build\src\gaxios.js:73:27)
      at Generator.next (&lt;anonymous&gt;)
      at fulfilled (C:\Users\Desktop\Nova pasta\node_modules\gaxios\build\src\gaxios.js:16:58)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
  (Use `node --trace-warnings ...` to show where the warning was created)
  (node:8392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
  (node:8392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>