Can't serve HTML on the Google Apps Script callback page in a GMail add-on

混江龙づ霸主 提交于 2021-01-28 18:03:28

问题


I am working on a GMail add-on that connects to a third-party service through OAuth2. To obtain the authorization code the following redirect URI is used: https://script.google.com/macros/d/[SCRIPT_ID]/usercallback . Here's a snippet that triggers authorization:

var stateToken = ScriptApp.newStateToken()
                          .withMethod( "authCallback" )
                          .withTimeout( 120 )
                          .createToken();

var authUrl = _authBaseUrl
              + "&client_id=" + encodeURIComponent( _clientId )
              + "&redirect_uri=" + encodeURIComponent( _redirectUri )
              + "&state=" + stateToken;

CardService.newAuthorizationException()
           .setAuthorizationUrl( authUrl )
           .setResourceDisplayName( "Resource" )
           .throwException();

And here's the callback function (the HTML snippet is taken from here):

function authCallback( request )
{
    createAccessToken( request.parameter.code );
    return HtmlService.createHtmlOutput('Success! <script>setTimeout(function() { top.window.close() }, 1);</script>');
}

The createAccessToken function gets successfully invoked and the add-on gets an access token. However, the HTML is not served in the popover window. Instead, there's a placeholder with the following error: "The script completed but did not return anything."

As a result, I am stuck with an error window which can't be closed automatically and doesn't tell user that they have to close the window to continue working with the add-on.

Is there anything wrong I am doing or this is some kind of bug or a dropped feature? Thanks for any suggestions.

UPD: The error looks like this


回答1:


Turns out the HtmlService actually works in that case. I had a function name collision for authCallback, so the proper one wasn't invoked. Sorry for misinformation. If anyone has trouble handling the callback window, do as described in the question.



来源:https://stackoverflow.com/questions/59860133/cant-serve-html-on-the-google-apps-script-callback-page-in-a-gmail-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!