如何下载我从Google Picker API中选择的文件


How to download a file that I have chosen from Google Picker API?

我正在PHP站点中实现GooglePicker。我可以从Google Picker API获取文件id,也可以使用JavaScript下载文件。下面是我在setCallback(pickerCallback)函数中调用的回调函数。

function pickerCallback(data) {
    if (data.action == google.picker.Action.PICKED) {
      var fileId = data.docs[0].id;
      document.getElementById('googleFileId').value = fileId;
      var name = data.docs[0].name;
      var url = data.docs[0].url;
      var accessToken = gapi.auth.getToken().access_token;
      var request = new XMLHttpRequest();
      request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + fileId);
      request.setRequestHeader('Authorization', 'Bearer ' + accessToken);
      request.addEventListener('load', function() {
          var item = JSON.parse(request.responseText);
          window.open(item.webContentLink,"_self"); //Download file in Client Side 
      });
      request.send();
    }
    var message = 'File ID of choosen file : ' + fileId;
    document.getElementById('result').innerHTML = message;
}

我可以将文件id传递给PHP,但要下载文件,我必须再次进行身份验证。有人能帮助我们如何用PHP进行文件下载吗?

谷歌开发者页面上有一个"管理下载"帮助,但对我不起作用https://developers.google.com/drive/web/manage-downloads.

发现了一个与此类似的问题,但没有回答如何在后台下载文件从谷歌选择器中选择文件后立即下载文件。

您必须为pick操作实现回调。看看我的实现:

    var buildPicker = function(parentId) {
      var pickerCallback = function(data) {
        if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED && data.viewToken[0] !== 'upload') {
          var docs = data[google.picker.Response.DOCUMENTS];
          for (var d = 0; d < docs.length; d++) {
            downloadFile(docs[d].id);
          }
        }
      };
      GAuth.getToken().then(function(token) {
        var picker = new $window.google.picker.PickerBuilder()
          .addView(new google.picker.DocsUploadView().setParent(parentId))
          .addView(new google.picker.DocsView().setParent(parentId).setIncludeFolders(true))
          .setDeveloperKey(apiKey)
          .setOAuthToken(token.access_token)
          .setCallback(pickerCallback);
        picker.enableFeature(google.picker.Feature.MULTISELECT_ENABLED);
        picker.build().setVisible(true);
      });
    };
    var downloadFile = function(fileId) {
      getFile(fileId).then(function(file) {
        var downloadUrl;
        if (angular.isDefined(file.exportLinks)) {
          downloadUrl = file.exportLinks['application/pdf'];
        } else {
          downloadUrl = file.webContentLink;
        }
        var $idown;
        var makeiFrame = function(url) {
          if ($idown) {
            $idown.attr('src', url);
          } else {
            $idown = $('<iframe>', {
              id: 'idown',
              src: url
            }).hide().appendTo('body');
          }
        };
        makeiFrame(downloadUrl);
      });
    };
     // Implemented with https://github.com/maximepvrt/angular-google-gapi. But any other implementation will be fine as well
    var getFile = function(fileId) {
      var parameters = {
        'fileId': fileId
      };
      return GApi.executeAuth('drive', 'files.get', parameters);
    };
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>