Google Picker API Invalid origin value error

问题描述

今天,Google Picker 在我的 Google Sheets 插件中停止工作,代码没有任何更改。模态对话中的错误内容如下:

无效的原始值。

控制台中的错误是:

无法在“DOMWindow”上执行“postMessage”:提供的目标源(“https://docs.google.com”)与收件人窗口的源(“https://n-a6p4dqsl***”)不匹配d6wq-0lu-script.googleusercontent.com')

正在丢弃 postMessage.. 来自意外的窗口

正在丢弃 postMessage.. 来自意外的窗口

加载“https://docs.google.com/picker?protocol=gadgets&origin=https%3A%2F%2Fdocs.google.com%2F&sdr=true&title&oauth_token=&developerKey=&hostId=n-a6p4dq***d6wq-0lu-script.googleusercontent.com&relayUrl=https%3A%2F%2Fn-a6p4dq***d6wq-0lu-script.googleusercontent.com%2Ffavicon.ico&nav= ((%22documents%22%2Cnull%2C%7B%22selectFolder%22%3Atrue%2C%22parent%22%3A%22root%22%7D)%2C(%22documents%22%2Cnull%2C%7B%22dr%22 %3Atrue%2C%22includeFolders%22%3Atrue%7D))&rpcService=qhurmoc5w4l7&rpctoken=xssf8g42xc2&thirdParty=true#rpctoken=xssf8g42xc2': 'ALLOW-FROM {{3}.} 不是公认的指令标题将被忽略。

可能是错误与我执行的这行代码有关setorigin()

        var picker = new google.picker.PickerBuilder()
            .addView(driveView)
            .addView(drivesView)
            .hideTitleBar()
            .setoAuthToken(token)
            .setDeveloperKey(DEVELOPER_KEY)
            .setCallback(pickerCallback)
        --> .setorigin(google.script.host.origin)
            .setSize(DIALOG_DIMENSIONS.width - 2,DIALOG_DIMENSIONS.height - 2)
            .build();

但这行代码直接来自 Google Picker API 的文档,并且之前工作正常。如果我更改 google.script.host.origin,将 https://docs.google.com 作为 URL 返回到 https://n-a6p4dqsl***6wcd6wq-0lu-script.googleusercontent.com,我会收到相同的错误一个错误,所以不是这样。

我也无法将其添加为 GCP 项目中的授权 javascript 源,因为它返回以下错误

无效的来源:使用了禁止

(这个https://docs.google.com/'

这似乎是一个错误,我无法在 Google 的问题跟踪器和 StackOverflow 上找到答案。

有人也遇到过这种情况或知道如何处理吗?

解决方法

告一段落,解决这个问题的唯一办法就是去掉后面的斜杠

来自

docs.google.com/

docs.google.com

相反,

google.script.host.orgin 给出了导致错误的“https://docs.google.com/”。因此你需要硬编码为

“https://docs.google.com”

Google 最近进行了一些更改,这可能会引发此问题。

更新

您可以使用此函数 - 并调用 - ...... setOrigin(getOrigin())

function getOrigin() {
    var url = google.script.host.origin;
    return url.substr(url.length - 1) === "/" ? url.substr(0,url.length - 1) : url;
}
,

在 iframe 中使用的解决方案

https://developers.google.com/apps-script/guides/dialogs#code.gs_2

代码.gs

         function showPicker() {
          var html = HtmlService.createHtmlOutputFromFile('dialog.html')
              .setWidth(600)
              .setHeight(425)
              .setSandboxMode(HtmlService.SandboxMode.IFRAME);
          SpreadsheetApp.getUi().showModalDialog(html,'Select a file');
    }


.setSandboxMode(HtmlService.SandboxMode.IFRAME); 
//can be removed or replaced with
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

对话框.html

function getOrigin() {
        var url = "https://mydomain.name/";
        return url.substr(url.length - 1) === "/" ? url.substr(0,url.length - 1) : url;
    }

更新

现在无需更改代码即可运行。