Google Apps脚本-在独立脚本上使用Google的文件选择器 [英] Google Apps Script - Using Google's file picker on a standalone script
问题描述
我正在创建一个应用程序,要求用户从其云端硬盘中选择一个文件夹.我正在努力设置Picker API.
I'm creating a application that requires the user to select a folder from their Drive. I'm struggling to set up the Picker API.
遵循该文档我使用他们的"Hello World"脚本设置了项目,但是更改'devlopedKey'和'clientID'后,我测试代码以接收错误:
Following this documentation I set up my project using their 'Hello World' script but after changing the 'devlopedKey' and 'clientID', I test the code to receive the error:
错误401,invalid_client,无注册来源.
Error 401, invalid_client, no registered origin.
在四处搜寻之后,我发现了一些建议,可以将客户端凭据中的Authorized JavaScript origin设置为 http://localhost:8888 .完成此操作后,我收到另一个错误:
After searching around I found suggestions to set the Authorised JavaScript origin within the client credential to http://localhost:8888. After doing this I receive a different error:
错误400,origin_mismatch
Error 400, origin_mismatch
对不起,如果这是我的简单错误,将不胜感激.
Sorry if this is a simple mistake of mine, any help would be appreciated.
推荐答案
您必须为Google Apps脚本专门设置setOrigin.
You have to setOrigin specifically for google apps script.
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);
以下是文档: https://developers.google.com /apps-script/guides/dialogs#file-open_dialogs
这篇关于Google Apps脚本-在独立脚本上使用Google的文件选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!