Google не определен при использовании скрипта приложения в приложении реакции - PullRequest
0 голосов
/ 06 января 2020

Я использую скрипт Google App, чтобы получить токен доступа пользователя OAuth 2.0 из серверного скрипта, чтобы он мог быть передан в Picker. Я скопировал вставленный код из Google Docs в скрипт приложения, который выглядит следующим образом.

//File code.gs
/**
 * Creates a custom menu in Google Sheets when the spreadsheet opens.
 */
function onOpen() {
  SlidesApp.getUi().createMenu('Picker')
      .addItem('Start', 'showPicker')
      .addToUi();
}

/**
 * Displays an HTML-service dialog in Google Sheets that contains client-side
 * JavaScript code for the Google Picker API.
 */
function showPicker() {
  var html = HtmlService.createTemplateFromFile('index.html').evaluate()
      .setWidth(600)
      .setHeight(425)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SlidesApp.getUi().showModalDialog(html, 'Select a file');
}

/**
 * Gets the user's OAuth 2.0 access token so that it can be passed to Picker.
 * This technique keeps Picker from needing to show its own authorization
 * dialog, but is only possible if the OAuth scope that Picker needs is
 * available in Apps Script. In this case, the function includes an unused call
 * to a DriveApp method to ensure that Apps Script requests access to all files
 * in the user's Drive.
 *
 * @return {string} The user's OAuth 2.0 access token.
 */
function getOAuthToken() {
  DriveApp.getRootFolder();
  return ScriptApp.getOAuthToken();
}

В моем приложении реагирования на нажатие кнопки я вызываю функцию, которая создает средство выбора

//index.js
getGDriveToken = async () => {
        const { userInfo } = this.props;
        console.log('Ise :', userInfo)
        if (userInfo.gdriveOAuth2Token) {

            gapi.load('picker', {'callback': function() {
                pickerApiLoaded = true;
            }});
            getOAuthToken();

            function getOAuthToken() {
                console.log('getOAuthToken');
                google.script.run.withSuccessHandler(createPicker)
                .withFailureHandler(showError).getOAuthToken();
            }

            function createPicker(token) {
                const appId = '';
                if (pickerApiLoaded && token) {
                    const uploadView = new google.picker.DocsUploadView();
                    var picker = new google.picker.PickerBuilder().
                            addViewGroup(
                                    new google.picker.ViewGroup(google.picker.ViewId.DOCS).
                                    addView(google.picker.ViewId.DOCUMENTS).
                                    addView(google.picker.ViewId.PRESENTATIONS)).
                                    addView(uploadView).
                                    setAppId(appId).
                                    setOAuthToken(token).
                                    setDeveloperKey(developerKey).
                                    setCallback(this.pickerCallback).
                                    setOrigin(google.script.host.origin).
                                    build();
                    picker.setVisible(true);
                }
            }
        }

    }

В моем индексе. html Я загрузил скрипт Google API

//index.html
  <script type="text/javascript" src="https://apis.google.com/js/api.js"></script>

Когда я пытаюсь загрузить сборщик, он выдает ссылку, ошибка Google не определена.

...