简体   繁体   English

如何从 Google Apps 脚本调用外部 API

[英]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.我想从 Google Apps 脚本 function 中调用 Steem API。我想看到适当的服务器响应。 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())你可以这样做: eval(UrlFetchApp.fetch("my url").getContentText())

How can I call the Steem API from inside Google Apps Script?我如何从 Google Apps 脚本中调用 Steem API?

Summary from comments:评论摘要:

Here is a similar but different SO question about importing modules.这是一个关于导入模块的类似但不同的 SO 问题

Summary from this article :本文摘要:

Using npm modules inside of Apps Script在 Apps 脚本中使用 npm 模块

2021-07-12 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.我最近在使用 Apps Script 处理一些数据,需要从一堆 URL 中解析出二级域名信息。 This is definitely not the job for regular expressions, but it's perfect for an npm module, psl, that uses the public suffix list.这绝对不是正则表达式的工作,但它非常适合使用公共后缀列表的 npm 模块 psl。

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.但是,虽然 Apps Script 已经取得了长足的进步,并且现在具有许多 ES2015+ 优点,但不可能从 npm 中提取任意代码并直接运行它。

To work around this, I created the following index.js file locally, exporting the interface that I wanted to call from Apps Script:为了解决这个问题,我在本地创建了以下 index.js 文件,导出了我想从 Apps 脚本调用的接口:

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 安装了必要的依赖项,并将这段代码与 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.我手动将 psl.js 文件的内容复制到 psl.gs 文件中,连同 Apps 脚本编辑器中的主 Code.gs 文件。 (This would be annoying if the bundled output changed frequently, but doing it once by hand wasn't a problem.) (如果捆绑的 output 经常更改,这会很烦人,但手动执行一次不是问题。)

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 Apps 脚本会自动使项目中所有 .gs 文件的内容在同一个全局 scope 中可见,所以我现在可以编写如下代码

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

inside of my main Code.gs file.在我的主要 Code.gs 文件中。

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.与 Node 相比,Apps Script 运行时环境仍然有很多怪癖,所以不要期望所有捆绑代码都能按原样工作。 Polyfills may be needed (I used url-parse, for instance, since the native URL object isn't available in Apps Script.)可能需要 Polyfill(例如,我使用了 url-parse,因为原生 URL object 在 Apps 脚本中不可用。)

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.一旦您将捆绑代码复制到 Apps 脚本,它就永远不会更新,因此请确保您准备好在您使用的模块有任何安全或功能更新时重新捆绑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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