Google Picker API 原始值错误无效

Google Picker API Invalid origin value error

今天 Google Picker 在我的 Google 表格插件中停止工作,代码没有任何更改。模态对话中的错误是:

Invalid origin value.

控制台中的错误是:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://docs.google.com') does not match the recipient window's origin ('https://n-a6p4dqsl***d6wq-0lu-script.googleusercontent.com')

dropping postMessage.. was from unexpected window

dropping postMessage.. was from unexpected window

Invalid 'X-Frame-Options' header encountered when loading 'https://docs.google.com/picker?protocol=gadgets&origin=https%3A%2F%2Fdocs.google.com%2F&sdr=true&title&oauth_token=<oathToken>&developerKey=<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 https://docs.google.com/' is not a recognized directive. The header will be ignored.

可能错误与我做的这行代码有关 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 选择器 API 的文档并且之前可以正常工作。如果我将 google.script.host.origin、returns https://docs.google.com 更改为 url 到 https://n-a6p4dqsl***6wcd6wq-0lu-script.googleusercontent.com,我会得到相同的错误和一个新错误,所以不是这样。

我也无法将其添加为 GCP 项目中的授权 javascript 来源,因为它 returns 出现以下错误:

Invalid Origin: uses a forbidden domain

(这个has been the case of a while)

这似乎是一个新错误,我在 Google 的问题跟踪器和 Whosebug 上都找不到答案。

有人也面临这个问题或知道如何处理吗?

结束,解决这个问题的唯一方法是删除

之后的尾部斜杠

来自

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

code.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);

dialog.html

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

更新

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