简体   繁体   English

如何从容器绑定脚本中调用GAS Execution API的run方法?

[英]How do i call GAS Execution API's run method from container bound script?

I have made an target project script as instructed by google in the documentation and followed all the steps for deploying as a API Executable.Enabled the Google apps script execution API also. 我已经按照google在文档中的说明制作了目标项目脚本,并按照将API可执行文件部署的所有步骤进行了操作。还启用了Google Apps脚本执行API。

In documentation, they have mentioned a Execution API's run method. 在文档中,他们提到了Execution API的run方法。 By using this we can call the projects method. 通过使用此方法,我们可以调用projects方法。 i don't know how to use this in GAS. 我不知道如何在GAS中使用它。

This is the target project's script, 这是目标项目的脚本,

/**
 * The function in this script will be called by the Apps Script Execution API.
 */

/**
 * Return the set of folder names contained in the user's root folder as an
 * object (with folder IDs as keys).
 * @return {Object} A set of folder names keyed by folder ID.
 */
function getFoldersUnderRoot() {
  var root = DriveApp.getRootFolder();
  var folders = root.getFolders();
  var folderSet = {};
  while (folders.hasNext()) {
    var folder = folders.next();
    folderSet[folder.getId()] = folder.getName();
  }
  return folderSet;
}

I'd tried following method to call execution API's run method, but it required access token, how can i get the access token in this code. 我试过以下方法来调用执行API的run方法,但是它需要访问令牌,如何在此代码中获取访问令牌。

var access_token=ScriptApp.getOAuthToken();//Returning a string value  
var url = "https://script.googleapis.com/v1/scripts/MvwvW29XZxP77hsnIkgD0H88m6KuyrhZ5:run";
var headers = {
  "Authorization": "Bearer "+access_token,
  "Content-Type": "application/json"
};
var payload = {
  'function': 'getFoldersUnderRoot',
  devMode: true
};
var res = UrlFetchApp.fetch(url, {
  method: "post",
  headers: headers,
  payload: JSON.stringify(payload),
  muteHttpExceptions: true
});

But in response i'm getting this, 但是作为回应,我得到了这个,

{
  "error": {
    "code": 401,
    "message": "ScriptError",
    "status": "UNAUTHENTICATED",
    "details": [
      {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionError",
        "errorMessage": "Authorization is required to perform that action.",
        "errorType": "ScriptError"
      }
    ]
  }
}

How can i solve this? 我该如何解决? Code explanation would be appreciable. 代码说明会很有意义。 Thank you. 谢谢。

How about this sample script? 这个示例脚本怎么样? From your question, I could know that you have already done deploying API Executable and enabling Execution API. 从您的问题中,我可以知道您已经完成了API可执行文件的部署和启用Execution API的工作。 In order to use Execution API, also an access token with scopes you use is required as you can see at Requirements. 为了使用Execution API,还需要具有使用范围的访问令牌,如“需求”中所示。 Have you already retrieved it? 您已经取回吗? If you don't have it yet, you can retrieve it by checking the Auth Guide . 如果还没有,可以通过查看Auth Guide来检索它。 By preparing these, you can use Execution API. 通过准备这些,您可以使用Execution API。

Following sample is for run getFoldersUnderRoot() in the project deployed API Executable. 以下示例用于在项目已部署的API可执行文件中运行getFoldersUnderRoot() Please copy and paste this sample script to your spreadsheet script editor. 请将此示例脚本复制并粘贴到电子表格脚本编辑器中。 Before you use this, please import your access token and script ID of the project deployed API Executable to the script. 使用此功能之前,请将您的访问令牌和项目已部署API可执行文件的脚本ID导入脚本。

Sample script : 示例脚本:

var url = "https://script.googleapis.com/v1/scripts/### Script ID ###:run";
var headers = {
  "Authorization": "Bearer ### acces token ###",
  "Content-Type": "application/json"
};
var payload = {
  'function': 'getFoldersUnderRoot',
  devMode: true
};
var res = UrlFetchApp.fetch(url, {
  method: "post",
  headers: headers,
  payload: JSON.stringify(payload),
  muteHttpExceptions: true
});

Result : 结果:

Here, the result of getFoldersUnderRoot() is returned as follows. 在这里, getFoldersUnderRoot()的结果返回如下。

{
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
    "result": {
      "### folder id1 ### ": "### folder name1 ###",
      "### folder id2 ### ": "### folder name2 ###",
    }
  }
}

暂无
暂无

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

相关问题 如何从渲染器进程调用Electron API方法? - How do I call an Electron API method from the renderer process? Photoshop:如何在 Photoshop 中从另一个 JSX 脚本运行/调用 JSX 脚本? - Photoshop: How do I run / call a JSX script from another JSX script in Photoshop? 如何从 iframe 作为其容器在 angular 中运行组件的方法 - How to run a component's method in angular from iframe as its container 尝试从侧边栏中使用 google.script.run 运行函数时,容器绑定脚本获取权限错误 - Container-bound Script getting permission errors trying to run functions with google.script.run from sidebar 是否可以从Google Apps脚本的库中调用容器绑定的脚本函数? - Is it possible to call container bound script functions from library in google apps script? 如何将变量从 Django 的 render 方法的上下文参数传递给前端的 javascript 脚本? - How do I pass a variable from Django's render method's context param to a javascript script on the frontend? 无法在GAS中使用JQuery从脚本调用脚本 - Can't call script from script with JQuery in GAS 如何从 Photoshop 脚本运行 Python 脚本? - How do I run a Python script from a Photoshop script? 使用NAN,如何从不是NAN_METHOD的C ++函数中调用Javascript? - Using NAN, how do I call Javascript from a C++ function that's not a NAN_METHOD? 如何从模板的外部调用Angularjs指令上的方法? - How do I call a method on an Angularjs directive from outside it's template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM