🏠 back to Observable

Importing googleapis

Hi, I am trying to import googleapis into ObservableHQ but have not had any luck - after trying the usual tools to diagnose. I see a window.GoogleApis to attach to (best looking candidate) but it does not appear to be a useable function f(options). From what I can see the examples have all tried to avoid importing this (using raw REST instead) - is it doable - if so is there an example I can look at? :slight_smile:

The googleapis package is Google’s API client for Node.js, so it’s doubtful that it’ll ever work in a browser. You probably want to use Google’s API client for JavaScript instead.

That client is (oddly) not open-source and not published to npm or versioned; it’s only available here:

https://apis.google.com/js/api.js

It uses the global export pattern, and in addition, it requires that you call gapi.load(callback) before you can use the library. So here’s how:

gapi = {
  const gapi = await require("https://apis.google.com/js/api.js").catch(() => window.gapi);
  await new Promise((callback, onerror) => gapi.load("client", {callback, onerror}));
  return gapi;
}
1 Like