如何使用Google Picker通过"drive.file"访问文件范围? [英] How do I use Google Picker to access files using the "drive.file" scope?

查看:41
本文介绍了如何使用Google Picker通过"drive.file"访问文件范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我集成了Google Drive的Web应用程序在 drive 范围内可以很好地工作,但是除非有必要,否则使用如此广泛的范围是不好的做法.我想将范围限制为 drive.file ,以便我只能访问由应用程序创建的文件和使用Google Picker打开的文件,但无法使其正常工作.

My Google Drive-integrated web application works fine with the drive scope, but using such a broad scope is bad practice unless necessary. I would like to restrict the scope to drive.file so that I can only access files created by the application and files opened using Google Picker, but I cannot get it to work.

由应用程序创建的文件可以打开而不会出现问题.但是,无法访问使用Google Picker打开的文件.尝试下载此类文件将导致404错误.当我右键单击Google云端硬盘中的文件并选择查看授权的应用程序"时,我的应用程序未列为授权的应用程序之一.

Files created by the application can be opened without problem. Files opened using Google Picker, however, are not accessible; attempting to download such a file results in a 404 error. When I right-click the file in Google Drive and select "View authorized apps", my application is not listed as one of the authorized apps.

如果将范围扩展到 drive ,则代码可以正常工作.

The code works fine if the scope is expanded to drive.

我写了一个最小测试页,该页面应下载用户选择的文件在Google Picker中.可以先调用 auth(),再调用 showPicker(),以启动该过程.该代码的关键部分如下:

I have written a minimal test page that should download a file selected by the user in Google Picker. The process can be started by calling auth() followed by showPicker(). The key parts of the code are as follows:

gapi.auth.authorize({
    client_id: '123456789012.apps.googleusercontent.com',
    scope: 'https://www.googleapis.com/auth/drive.file',
    immediate: false
});

...

var picker = new google.picker.PickerBuilder()
    .setAppId('123456789012')
    .addView(new google.picker.DocsView(google.picker.ViewId.DOCS_IMAGES))
    .setOAuthToken(gapi.auth.getToken().access_token)
    .setCallback(onPickerAction)
    .build();
picker.setVisible(true);

...

function onPickerAction(data) {
    if ( data.action === google.picker.Action.PICKED ) {
        var id = data.docs[0].id;
        var request = new XMLHttpRequest();
        request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
        request.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);

        request.addEventListener('load', function() {
            var item = JSON.parse(request.responseText);
            console.log(item);
        });

        request.send();
    }
}

相关问题得出结论,应用ID设置不正确.那似乎并没有影响我.我已经测试了所有可以想到的组合,但没有任何运气.

A related question concluded that the app ID was improperly set. That does not seem to affect me; I have tested all combinations I can think of without any luck.

推荐答案

在Google+中首先建议对话,该问题可以通过以下方式解决:

As first suggested in a Google+ conversation, the problem can be solved as follows:

  1. 确保已在Google开发者控制台中启用Drive SDK(Drive API 是另一种功能,仅启用该功能还不够).
  2. 为您的应用程序指定打开URL".
  1. Make sure that the Drive SDK is enabled in the Google developer console (the Drive API is something else, and only having that enabled is not enough).
  2. Specify an "Open URL" for your application.

这篇关于如何使用Google Picker通过"drive.file"访问文件范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆