[英]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导入脚本。
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
});
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.