Convert Contact URL to Resource Name for People API using Google Apps Script

Gists

This is a sample script for converting a contact URL of “person/c###” to a resource name “people/c###” for People API using Google Apps Script.

When you open each contact at Contacts with your browser, you can see the URL like https://contacts.google.com/person/c###################.

Here, you might have a situation where you are required to retrieve the resource name for People API from this URL. But, in the current stage, person/c################### cannot be directly used as the resource name people/c###################. In this report, I would like to introduce a sample script for converting from person/c################### to people/c###################.

Usage

1. Create a Google Apps Script project

Please create a standalone Google Apps Script project. Of course, this script can be also used with the container-bound script.

And, please open the script editor of the Google Apps Script project.

2. Enable People API

Please enable People API at Advanced Google services. Ref

3. Get the URL from the contact

Please open Contacts with your browser. And, as a sample value for testing the script, please retrieve the contact URL of a user. It’s like https://contacts.google.com/person/c###################. This URL is used.

4. Sample script

Please copy and paste the following script to the script editor. And, please set your URL to contactUrl.

function myFunction() {
  // Please set your contact URL.
  const contactUrl = "https://contacts.google.com/person/c###################";

  // ref: https://stackoverflow.com/a/21668344
  function dec2hex(str) {
    // .toString(16) only works up to 2^53
    var dec = str.toString().split(""),
      sum = [],
      hex = [],
      i,
      s;
    while (dec.length) {
      s = 1 * dec.shift();
      for (i = 0; s || i < sum.length; i++) {
        s += (sum[i] || 0) * 10;
        sum[i] = s % 16;
        s = (s - sum[i]) / 16;
      }
    }
    while (sum.length) {
      hex.push(sum.pop().toString(16));
    }
    return hex.join("");
  }

  // Convert large decimal to hexadecimal.
  const hexadecimalValue = dec2hex(
    contactUrl.replace("https://contacts.google.com/person/c", "")
  );

  // Search contact.
  let res = null;
  let pageToken = "";
  do {
    const obj = People.People.Connections.list("people/me", {
      personFields: "emailAddresses",
      pageSize: 1000,
      pageToken,
    });
    if (obj.connections.length > 0) {
      const t = obj.connections.find((c) =>
        c.emailAddresses.some((e) => e.metadata.source.id == hexadecimalValue)
      );
      if (t) {
        res = t;
        break;
      }
    }
    pageToken = obj.nextPageToken;
  } while (pageToken);
  if (!res) return;

  // Return resource name.
  const { resourceName } = res;
  console.log(resourceName);
}

When this script is run, the resource name of people/c################### for using People APi is obtained from a contact URL https://contacts.google.com/person/c###################. This resource name can be used to People API.

Reference

 Share!