[英]How to run the triggers in Google app script without reauth each time
Hello I have a script which is not a web app but it is an API based one it fetch data from json and sent them to blogger as post , and I am running this function with time based trigger , but after some run I need to open the auth url and get verification .. How can I avoid that and run the triggers continues This is the auth code 您好,我有一个不是Web应用程序的脚本,但它是一个基于API的脚本,它从json提取数据并将它们作为post发送给Blogger,并且我正在使用基于时间的触发器来运行此功能,但是在运行一些之后,我需要打开身份验证网址并获得验证..如何避免这种情况并继续运行触发器,这是身份验证代码
var service = getBloggerService_();
if (service.hasAccess()) {
var api ='https://www.googleapis.com/blogger/v3/blogs/' + blogId + '/posts/';
var headers = {
'Authorization': 'Bearer ' + getBloggerService_().getAccessToken()
};
var options = {
'headers': headers,
'method' : 'post',
'contentType': 'application/json',
'payload': body,
'muteHttpExceptions': false
};
try {
var response = UrlFetchApp.fetch(api, options);
var responseCode = response.getResponseCode();
Logger.log(responseCode);
var json = JSON.parse(response.getContentText());
Logger.log(json);
}
catch(err) {
Logger.log(err); // error with url fetch call
}
}
else {
var authorizationUrll=service.getAuthorizationUrl();
Logger.log('Open the following URL and
re-run the script: %s',
authorizationUrl);
} } }}
Add the Blogger scope in appsscript.json manually. 手动将Blogger范围添加到appsscript.json中。
{ ... "oauthScopes": [ "https://www.googleapis.com/auth/blogger", "https://www.googleapis.com/auth/script.external_request" ], ... }
You can then use: 然后,您可以使用:
var headers = { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(); //contains Blogger scope always };
You don't need to use service
or any other library. 您不需要使用service
或任何其他库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.