【问题标题】:Unable to open FilePicker with Google App script无法使用 Google App 脚本打开 FilePicker
【发布时间】:2018-12-23 15:06:58
【问题描述】:

我尝试使用 Google 应用脚本。我的第一次尝试涉及打开文件选择器并从我的 Google Drive 中选择文件的能力。为此,我阅读了official documentation 并尝试实现它。所以我编写了以下应用程序:

代码.gs

function doGet() {
  return HtmlService
      /*.createTemplateFromFile('index.html')
      .evaluate();*/
      .createHtmlOutputFromFile('index.html');
}

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Picker Example</title>

    <script type="text/javascript">

    // The Browser API key obtained from the Google API Console.
    // Replace with your own Browser API key, or your own key.
    var developerKey = "{{developer-key}}";                        

    // The Client ID obtained from the Google API Console. Replace with your own Client ID.
    var clientId = "{{client-id}}"

    // Replace with your own project number from console.developers.google.com.
    // See "Project number" under "IAM & Admin" > "Settings"
    var appId = "{{project-number}}";

    // Scope to use to access user's Drive items.
    var scope = ['https://www.googleapis.com/auth/drive'];

    var pickerApiLoaded = false;
    var oauthToken;

    // Use the Google API Loader script to load the google.picker script.
    function loadPicker() {
      gapi.load('auth', {'callback': onAuthApiLoad});
      gapi.load('picker', {'callback': onPickerApiLoad});
    }

    function onAuthApiLoad() {
      window.gapi.auth.authorize(
          {
            'client_id': clientId,
            'scope': scope,
            'immediate': false
          },
          handleAuthResult);
    }

    function onPickerApiLoad() {
      pickerApiLoaded = true;
      createPicker();
    }

    function handleAuthResult(authResult) {
      if (authResult && !authResult.error) {
        oauthToken = authResult.access_token;
        createPicker();
      }
    }

    // Create and render a Picker object for searching images.
    function createPicker() {
      if (pickerApiLoaded && oauthToken) {
        var view = new google.picker.View(google.picker.ViewId.DOCS);
        view.setMimeTypes("image/png,image/jpeg,image/jpg");
        var picker = new google.picker.PickerBuilder()            
            .enableFeature(google.picker.Feature.NAV_HIDDEN)
            .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
            .setAppId(appId)
            .setOAuthToken(oauthToken)
            .addView(view)
            .addView(new google.picker.DocsUploadView()) 
            .setDeveloperKey(developerKey)
            .setCallback(pickerCallback)
            .build();
         picker.setVisible(true);
      }
    }

    // A simple callback implementation.
    function pickerCallback(data) {
      if (data.action == google.picker.Action.PICKED) {
        var fileId = data.docs[0].id;
        alert('The user selected: ' + fileId);
      }
    }
    </script>
  </head>
  <body>
    <div id="result"></div>

    <!-- The Google API Loader script. -->
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script>
  </body>
</html>

当我尝试执行代码时,在授权阶段之后,我获得了一个空白屏幕和一个空白对话框,并出现错误:无法执行'postMessage'。怎么了?谢谢

【问题讨论】:

    标签: javascript google-apps-script web-applications google-picker


    【解决方案1】:

    问题/解决方案:

    您在 iframe 中工作,其中的内容来自不同的来源。您应该在选择器生成器上使用setOrigin。

    片段:

    var picker = new google.picker.PickerBuilder()
       //...
      .setOrigin(google.script.host.origin)
    

    参考:

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 2012-12-27
      • 2011-02-03
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多