Google应用脚本授权来自html文件的脚本不起作用

时间:2017-04-15 17:24:13

标签: javascript html google-apps-script

我正在尝试制作Google Apps脚本网络应用。我的目标是向用户显示一个HTML文件,并让HTML文件将一些数据发送回脚本,以便它可以修改Google文档。我有.gs文件,代码如下:

function doGet() {
  var html = HtmlService.createTemplateFromFile("index").evaluate();
  html.setTitle("Test");
  return html; 
}

function test(url, text) {  
    DocumentApp.openByUrl(url).getBody().doSomethingHere
}

我还有以下html文件(index.html):

<html lang="en">

<head>
    <meta name="google-signin-scope" content="profile email https://www.googleapis.com/auth/documents https://www.googleapis.com/auth/script.external_request">
    <meta name="google-signin-client_id" content="123456789012-someidhere.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <script src="https://apis.google.com/js/api.js" async defer></script>
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <script>
        function getValue() {
            var text = document.getElementById("text").value;
            var url = document.getElementById("url").value;
            google.script.run.test(url, text);
        }
    </script>
</head>

<body>
    <div class="g-signin2" id="signin-button" data-onsuccess="onSignIn" data-theme="dark"></div>
    <div id="status"></div>
    <br>
    <br>
    <br>
    <style>
        #signin-button,
        #status {
            float: left;
            display: inline-block;
            padding-left: 5px;
        }
    </style>
    <script>
        function onSignIn(googleUser) {
            // Useful data for your client-side scripts:
            var profile = googleUser.getBasicProfile();
            document.getElementById('status').innerHTML = 'Signed in as ' + profile.getName() + ' (' + profile.getEmail() + ')';

            // The ID token you need to pass to your backend:
            var id_token = googleUser.getAuthResponse().id_token;
            console.log("ID Token: " + id_token);
        };
    </script>
    <input type="text" id="url" placeholder="Document URL" style="width: 100%;" />
    <BR>
    <input type="text" id="text" />
    <input type="button" value="OK" onclick="getValue();" />
</body>

</html>

但是,当我输入值并按“确定”时,我会在控制台上收到错误消息:

未捕获错误:不允许操作  在测试(代码:9)(我的测试脚本)

我已经看到了一些建议,以确保用户转到脚本并对其进行授权。我不想这样做。有没有办法使用OAuth 2.0登录按钮执行此操作,因此我不必让用户转到脚本本身?提前谢谢!

0 个答案:

没有答案