谷歌网络应用中的谷歌驱动器选择器

时间:2015-07-19 18:56:22

标签: google-apps-script google-drive-api picker

我要做的是在我的Google Web应用中显示Google Picker。我已经尝试了很多方法来实现这一目标,但没有任何作用。

目前我的代码看起来像这样:

WebApp.html

<!-- rest of the code -->

<button type="button" id="pick">Pick File</button>
</div>
    <script>
        function initPicker() {
            var picker = new FilePicker({
                apiKey: "####################",
                clientId: "##########-##########################",
                buttonEl: document.getElementById('pick'),
                onSelect: function(file) {
                    alert('Selected ' + file.title);
                } // onSelect
            }); // var picker
        } // function initPicker()
    </script>

 <!-- rest of the code -->

WebAppJS.html

  /* rest of the code */

var FilePicker = window.FilePicker = function(options) {
    this.apiKey = options.apiKey;
    this.clientId = options.clientId;
    this.buttonEl = options.buttonEl;
    this.onSelect = options.onSelect;
    this.buttonEl.addEventListener('click', this.open.bind(this));
    this.buttonEl.disabled = true;
    gapi.client.setApiKey(this.apiKey);
    gapi.client.load('drive', 'v2', this._driveApiLoaded.bind(this));
    google.load('picker', '1', { callback: this._pickerApiLoaded.bind(this) });
}

FilePicker.prototype = {
open: function() {
var token = gapi.auth.getToken();
if (token) {
this._showPicker();
} else {
this._doAuth(false, function() { this._showPicker(); }.bind(this));
}
},
_showPicker: function() {
var accessToken = gapi.auth.getToken().access_token;
this.picker = new google.picker.PickerBuilder().
addView(google.picker.ViewId.DOCUMENTS).
setAppId(this.clientId).
setOAuthToken(accessToken).
setCallback(this._pickerCallback.bind(this)).
build().
setVisible(true);
},

_pickerCallback: function(data) {
  if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
    var file = data[google.picker.Response.DOCUMENTS][0],
    id = file[google.picker.Document.ID],
    request = gapi.client.drive.files.get({ fileId: id });
    request.execute(this._fileGetCallback.bind(this));
  }
},

_fileGetCallback: function(file) {
  if (this.onSelect) {
    this.onSelect(file);
  }
},

_pickerApiLoaded: function() {
  this.buttonEl.disabled = false;
},

_driveApiLoaded: function() {
  this._doAuth(true);
},

_doAuth: function(immediate1, callback) {
  gapi.auth.authorize({
    client_id: this.clientId + '.apps.googleusercontent.com',
    scope: 'https://www.googleapis.com/auth/drive.readonly',
    immediate: immediate1
  }, callback);
}
}; // FilePicker.prototype

/* rest of the code */

目前,此代码的作用是显示一种弹出窗口,但是为空。代码基于Daniel15's code

我已经尝试过的是:

  • 将代码块重新定位到服务器端和客户端,

  • 使用htmlOutput,htmlTemplate - 不是那些作品,

  • 许多其他事情,我无法记住。

我想得到的是回答问题:为什么此代码不显示Google Picker。

提前致谢,Mentozz。

1 个答案:

答案 0 :(得分:2)

尝试添加通话来源和开发者密钥

_showPicker: function() {
  var accessToken = gapi.auth.getToken().access_token;
  this.picker = new google.picker.PickerBuilder()
  .addView(google.picker.ViewId.DOCUMENTS)
  .setAppId(this.clientId)
  .setOAuthToken(accessToken)
  .setCallback(this._pickerCallback.bind(this))

  .setOrigin('https://script.google.com') //
  .setDeveloperKey(BROWSERKEYCREATEDINAPICONSOLE) //

  .build()
  .setVisible(true);
},