简体   繁体   中英

How to call an external API from Google Apps Script

I want to call the Steem API from inside a Google Apps Script function. I want to see the appropriate server response. But I get the following error instead.

气体错误

According to this documentation , I ran the following test.

// @see https://github.com/steemit/steem-js/tree/master/doc#browser

const getHelloWorld = () => {
  const SOURCE_CODE_URL = 'https://cdn.jsdelivr.net/npm/steem/dist/steem.min.js';
  const response = UrlFetchApp.fetch( SOURCE_CODE_URL, );
  const code = response.getContentText();
  const steem = eval( code, );

  steem.api.getAccounts([ 'ned', 'dan', ], ( error, response, ) => {
    Logger.log( error, response, );
  });
}

In my prior research, I read that this answer says:

You can do this: eval(UrlFetchApp.fetch("my url").getContentText())

How can I call the Steem API from inside Google Apps Script?

Summary from comments:

Here is a similar but different SO question about importing modules.

Summary from this article :

Using npm modules inside of Apps Script

2021-07-12

I recently was processing some data using Apps Script, and needed to parse out second-level domain info from a bunch of URLs. This is definitely not the job for regular expressions, but it's perfect for an npm module, psl, that uses the public suffix list.

But while Apps Script has come a long way, and features lots of ES2015+ goodness nowadays, it's not possible to pull in arbitrary code from npm and run it directly.

To work around this, I created the following index.js file locally, exporting the interface that I wanted to call from Apps Script:

import psl from 'psl';
import Url from 'url-parse';

export function parseHostname(sourceURL) {
  const url = new Url(sourceURL);
  return psl.parse(url.hostname);
}

Then, I installed the necessary dependencies from npm, and bundled this code up with esbuild:

npm init -y
npm install --save-dev psl url-parse punycode
npx esbuild index.js --bundle --global-name=psl --outfile=psl.js

I manually copied the contents of the psl.js file into a psl.gs file, alongside my main Code.gs file in the Apps Script editor. (This would be annoying if the bundled output changed frequently, but doing it once by hand wasn't a problem.)

Apps Script will automatically make the contents of all.gs files in a project visible in the same global scope, so I could now write code like

const {sld} = psl.parseHostname(url);

inside of my main Code.gs file.

Caveats

The Apps Script runtime environment still has a bunch of quirks when compared to Node, so don't expect all of your bundled code to work as-is. Polyfills may be needed (I used url-parse, for instance, since the native URL object isn't available in Apps Script.)

Once you copy over the bundled code to Apps Script, it's never going to be updated, so make sure you're prepared to rebundle if and when there are any security or feature updates to the modules you're using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM