Google Drive Picker API-显示文件夹和某些MIME类型文件

时间:2019-12-11 21:18:12

标签: google-drive-api google-picker

我已经设置了仅显示图像和.pdf文件的Google Picker API,但是我想设置此显示文件夹,我尝试了以下代码:

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

    function createPicker() {
          if (pickerApiLoaded && oauthToken) {
            //var view = new google.picker.View(google.picker.ViewId.DOCS); ... the mime type filter works only with DOCS
            var view = new google.picker.DocsView(google.picker.ViewId.FOLDERS);
            view.setMimeTypes("application/pdf,image/png,image/jpeg,image/jpg,application/vnd.ms-excel");

            var picker = new google.picker.PickerBuilder()
                .enableFeature(google.picker.Feature.NAV_HIDDEN)
                .enableFeature(google.picker.Feature.SUPPORT_DRIVES)
                .addView(view)
                .setAppId(appId)
                .setOAuthToken(oauthToken)
                .setDeveloperKey(developerKey)
                .setCallback(pickerCallback)
                .build();
            picker.setVisible(true);
          }
        }

使用此代码,Picker可以工作,问题在于每个文件夹中都显示了所有类型的文件(Google文档文件,zip,js等)

这些是使用的范围:

var scope = [
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.metadata',
        'https://www.googleapis.com/auth/drive.readonly'
    ];

我可以显示Google Picker API中的文件夹,并且仅在其中显示我在setMimeTypes()过滤器中设置的文件吗?

1 个答案:

答案 0 :(得分:0)

您可能需要向视图添加.setIncludeFolders(true)。例如:

    var viewPdf = new google.picker.DocsView(google.picker.ViewId.PDFS).setIncludeFolders(true);
    var viewPresentation = new google.picker.DocsView(google.picker.ViewId.PRESENTATIONS).setIncludeFolders(true);
    var picker = new google.picker.PickerBuilder().
            addView(viewPdf).
            addView(viewPresentation).
            setOAuthToken(oauthToken).
            setDeveloperKey(developerKey).
            setCallback(pickerCallback).
            build();
        picker.setVisible(true);