【问题标题】:Is OAuth possible from a Google Apps Script gadget on a site?是否可以通过网站上的 Google Apps 脚本小工具进行 OAuth?
【发布时间】:2023-04-03 02:24:01
【问题描述】:

我在使用 oAuth 的电子表格中开发了一个 google 应用程序脚本。 我现在将相同的脚本复制到在站点上运行的小工具中。当我想运行使用 oauth 的函数时,出现以下错误:

序列化延续时的意外异常

当我在网站上运行实际小工具或从脚本编辑器运行函数时,都会发生这种情况。从电子表格中的脚本编辑器调用完全相同的代码。 是我做错了什么,还是在使用站点小工具时根本无法将 oAuth 与 UrlFetchApp.fetch 一起使用?

谢谢,

一月

这是我正在尝试做的一些示例代码,您需要包含来自 Google Api 控制台的真实 api 机密来测试它。

  function CalendarApiBug( ) {

    var oAuthConfig = UrlFetchApp.addOAuthService('agenda scheduler');
    oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/"+
                                   "OAuthGetRequestToken?scope=https://www.googleapis.com/auth/calendar");
    oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
    oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
    oAuthConfig.setConsumerKey('replacemewithsomethingreal');
    oAuthConfig.setConsumerSecret('replacemewithsomethingreal');

    this.baseUrl = 'https://www.googleapis.com/calendar/v3/';
    this.calendarsList = null;

    this.getBaseUrl = function() {
      return this.baseUrl;
    } //CalendarApiBug.getBaseUrl

    this.getFetchArgs = function() {
      return {oAuthServiceName:'agenda scheduler', oAuthUseToken:"always"};
    } //CalendarApiBug.getFetchArgs

    this.getCalendarList = function(refresh){
      if (refresh != true && this.calendarsList != null )
        return this.calendarsList;

      var fetchArgs = this.getFetchArgs();   
      fetchArgs.method = 'get';
      var url = this.baseUrl + 'users/me/calendarList'; 
      this.calendarsList = Utilities.jsonParse(UrlFetchApp.fetch(url, fetchArgs).getContentText());
      return this.calendarsList; 
    } //CalendarApiBug.getCalendarList
  }

  function test(){
    var api = new CalendarApiBug();
    Logger.log(api.getCalendarList(false));
  }

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    oAuth 批准对话框仅在从脚本管理器中运行代码时才可见。为了将您的 Apps 脚本代码发布到站点,您需要将该版本的脚本作为服务发布。打开该脚本的代码编辑器,并确保您可以先使用脚本编辑器运行这些函数。这将验证您的 oAuth 批准是否已存储。

    【讨论】:

    • 我尝试先在编辑器中运行这些函数,但它给出了错误。我认为脚本已经发布,因为小工具的 UI 显示在我的网站上。只要我不调用 oauth 代码,它就可以工作。
    【解决方案2】:

    为什么在日历服务的 Google Apps 脚本中使用 Oauth?

    【讨论】:

    • 因为通过 oauth 提供的日历 API 允许的不仅仅是内置 API,例如更改日历的 ACL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多