Google Apps Script - Using Google's file picke

2019-08-02 14:58发布

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.

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:

Error 401, invalid_client, no registered origin.

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:

Error 400, origin_mismatch

Sorry if this is a simple mistake of mine, any help would be appreciated.

1条回答
Anthone
2楼-- · 2019-08-02 15:31

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

Here is the documentation: https://developers.google.com/apps-script/guides/dialogs#file-open_dialogs

查看更多
登录 后发表回答