Google Apps Script - 在独立脚本上使用 Google 的文件选择器

Con*_*nor 1 google-api google-apps-script

我正在创建一个应用程序,要求用户从他们的云端硬盘中选择一个文件夹。我正在努力设置 Picker API。

按照本文档,我使用他们的“Hello World”脚本设置了我的项目,但是在更改了“devlopedKey”和“clientID”之后,我测试了代码以接收错误:

错误 401,invalid_client,没有注册的来源。

在四处搜索后,我发现了将客户端凭据中的 Authorized JavaScript origin 设置为http://localhost:8888 的建议。这样做后,我收到一个不同的错误:

错误 400,origin_mismatch

对不起,如果这是我的一个简单错误,任何帮助将不胜感激。

Jor*_*hea 7

你必须专门为谷歌应用程序脚本设置原点。

var picker = new google.picker.PickerBuilder()
            // Instruct Picker to display only spreadsheets in Drive. For other
            // views, see https://developers.google.com/picker/docs/#otherviews
            .addView(google.picker.ViewId.SPREADSHEETS)
            // Hide the navigation panel so that Picker fills more of the dialog.
            .enableFeature(google.picker.Feature.NAV_HIDDEN)
            // Hide the title bar since an Apps Script dialog already has a title.
            .hideTitleBar()
            .setOAuthToken(token)
            .setDeveloperKey(DEVELOPER_KEY)
            .setCallback(pickerCallback)
//THIS IS THE IMPORTANT LINE FOR YOU
            .setOrigin(google.script.host.origin)
            // Instruct Picker to fill the dialog, minus 2 pixels for the border.
            .setSize(DIALOG_DIMENSIONS.width - 2,
                DIALOG_DIMENSIONS.height - 2)
            .build();
        picker.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

这是文档:https : //developers.google.com/apps-script/guides/dialogs#file-open_dialogs