简体   繁体   English

如何在Google应用程序脚本中运行触发器而无需每次都重新认证

[英]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);

} } }}

Solution: 解:

  • 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或任何其他库。

References: 参考文献:

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

相关问题 如何在准确的时间运行谷歌应用脚​​本? - How to run google app script at exact time? 在 Google Addon(Google App Script)中实现可安装触发器的正确方法 - Correct way to implement Installable Triggers in Google Addon (Google App Script) Google Script 上的触发器问题 - Issues with Triggers on Google Script 如何减少此脚本的执行时间(Google应用脚本超时) - How to reduce execution time of this script (Google app script timeout) Google Apps 脚本:时间驱动触发器时区问题 - Google Apps Script: Time Driven Triggers timezone issue 如何刷新标签而不重新加载Google App脚本中的页面 - How to refresh tag without reloading page in google app script Google App脚本-如何在不定义对象的情况下使用方法 - Google App Script - How to use methods without defining their object 如何让多个函数连续运行 - 谷歌应用程序脚本 - How to get multiple functions to run in a row - google app script 通过Drive API自动执行Google表格操作(制作副本,Google App脚本,触发器) - Automating Google Sheet actions (Make a copy, Google App Script, Triggers) via Drive API 如何将具有触发器和功能的脚本连接到多个Google表格? - How to Connect a Script with Triggers and Functions to Multiple Google Sheets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM