1

我在使用 oAuth 的电子表格中开发了一个谷歌应用程序脚本。我现在将相同的脚本复制到在站点上运行的小工具中。当我想运行使用 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));
  }
4

2 回答 2

1

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

于 2012-05-22T19:40:31.037 回答
0

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

于 2012-05-23T12:30:09.690 回答