Google apps script 哪些是在应用程序脚本API上运行请求的正确作用域
我正在使用应用程序脚本API运行带有服务帐户凭据的函数。 我在Rest资源API中添加了所有必需的作用域 但是当我在下面运行这个脚本时,它失败了Google apps script 哪些是在应用程序脚本API上运行请求的正确作用域,google-apps-script,google-oauth,service-accounts,google-apps-script-api,Google Apps Script,Google Oauth,Service Accounts,Google Apps Script Api,我正在使用应用程序脚本API运行带有服务帐户凭据的函数。 我在Rest资源API中添加了所有必需的作用域 但是当我在下面运行这个脚本时,它失败了 function run(){ var CREDENTIALS = { "private_key": "Your Private key", "client_email": "Your Client email", "client_id"
function run(){
var CREDENTIALS = {
"private_key": "Your Private key",
"client_email": "Your Client email",
"client_id": "Your Client ID",
"user_email": "Your Email address",
"api_key": "Your API key"
};
var service = getService(CREDENTIALS.client_email,CREDENTIALS.private_key);
service.reset();
if (service.hasAccess()) {
var url = 'https://script.googleapis.com/v1/projects/[SCRIPT ID]:run';
var body = {
"function": [FUNCTION NAME]
};
var params = {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
method: 'post',
playload : JSON.stringify(body),
contentType: 'application/json',
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, params);
Logger.log(response);
}
else {
Logger.log(service.getLastError());
}
}
function getService(email, privateKey) {
return OAuth2.createService('Service Account')
// Set the endpoint URL.
.setTokenUrl('https://oauth2.googleapis.com/token')
// Set the private key and issuer.
.setPrivateKey(privateKey)
.setIssuer(email)
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
.setSubject([USER EMAIL])
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope('https://www.googleapis.com/auth/script.external_request');
}
我有一个404错误,我想它来自scopes列表。
因此,我无法使用OAuth2.0令牌运行作为API可执行文件部署的脚本。
我应该选择哪些作用域通过HTTP请求运行函数?在您的
运行
函数中,对于参数
对象,您应该拥有有效负载
而不是游戏加载
- 您想将应用程序脚本API与服务帐户一起使用
- 您希望使用谷歌应用程序脚本实现这一点
- 我认为当您使用OAuth2时,非常有用
- 您希望让多个用户以您的所有者身份运行脚本
myFunction()
的函数,请放入以下示例脚本
function doGet(e) {
var values = e; // When you want to give the values by requesting, you can use the event object like "e".
var res = myFunction(values);
return ContentService.createTextOutput(res);
}
- 在这种情况下,使用GET方法。如果只想运行函数,可以使用此脚本。如果要通过提供大数据来运行函数,可以使用
而不是doPost()
doGet()
- 这样,脚本将作为所有者运行
- 这里,当设置“Anyone”时,脚本将作为每个用户运行。在这种情况下,需要将脚本共享给每个用户。并且需要使用访问令牌。请小心这个
- 在这种情况下,请求时不需要访问令牌。我认为作为测试用例,我推荐这种设置
- 当然,您也可以使用访问令牌。此时,请将其设置为“任何人”
https://script.google.com/macros/s/###/exec
。
- 当您修改Google Apps脚本时,请重新部署为新版本。这样,修改后的脚本将反映到Web应用程序中。请注意这一点。
myFunction
。请设置您的Web应用URL。在上述Web应用的设置中,每个用户都可以通过以下curl命令进行访问
curl -GL \
-d "key=value" \
"https://script.google.com/macros/s/###/exec"
- 当
用作上述查询参数时,在key=value
处,您可以使用doGet(e)
检索e.parameter.key
value
Publish>deployaswebapp
。将执行应用程序设置为:
字段设置为Me(youremail)
。通过这种方式,您可以将脚本作为浏览器链接共享,任何用户都可以使用您的凭据运行脚本
您可以添加带有确认消息的某个用户界面,以便用户知道他们已成功执行脚本。您可以在此找到文档。Hi@yoxCL9,您确定url正确吗?它是scripts/{scriptId}:run,而您的脚本似乎错过了
/scripts/
路径部分,从而错过了404错误代码。如果authHi@OlegValter有问题,你应该会收到403,我的错,我之前用projects.get方法检查了一个请求,它可以正常工作。Hi@yoxCL9-我明白了-尽管它看起来与Tanaike的回答没有关系,因为他详细说明了实际的潜在问题(哈,我怎么可能错过了不支持的服务帐户)很抱歉,我错过了此警告,因此如何使用所有者用户凭据与多个用户一起运行脚本?谢谢@Tanaike,但是Google应用程序脚本项目已链接到云平台表单项目,应用程序名称已填入API&Services>Credentials>OAuth同意屏幕。@yoxCL9从您的问题中,我提出了应用程序使用OAuth2编写API脚本。