【问题标题】:how to change the accessibility of Google Drive file on insert the file using Javascript Google Picker API如何在使用 Javascript Google Picker API 插入文件时更改 Google Drive 文件的可访问性
【发布时间】:2016-11-10 16:39:31
【问题描述】:

我尝试使用 Google Drive Picker API 上传新图片或将图片插入到我的 CMS。

它正在工作。我可以获取图片网址。但问题是:在 Google Drive 上上传的文件的可访问性默认是私有的(只有我作为所有者可以通过登录 Google 帐户查看它)。但是,当我从 Google Drive 站点手动将文件的可访问性设置更改为 Public 或任何有链接的人 时,任何人都可以在浏览器上查看这些图像。

我的意思是:如何在使用 Google Picker javascript API 插入文件时更改 Google Drive 文件的可访问性(将可访问性更改/设置为公共或任何有链接的人)。以下是我当前的代码。

<script>
    //<![CDATA[
    (function() {
        /**
         * Initialise a Google Driver file picker
         */
        var FilePicker = window.FilePicker = function(options) {
            // Config
            this.apiKey = options.apiKey;
            this.clientId = options.clientId;

            // Elements
            this.buttonEl = options.buttonEl;

            // Events
            this.onSelect = options.onSelect;

            this.buttonEl.addEventListener('click', this.open.bind(this));

            // Disable the button until the API loads, as it won&#39;t work properly until then.
            this.buttonEl.disabled = true;

            // Load the drive API
            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 the file picker.
             */
            open: function() {
                // Check if the user has already authenticated
                var token = gapi.auth.getToken();
                if (token) {
                    this._showPicker();
                } else {
                    // The user has not yet authenticated with Google
                    // We need to do the authentication before displaying the Drive picker.
                    this._doAuth(false, function() {
                        this._showPicker();
                    }.bind(this));
                }
            },

            /**
             * Show the file picker once authentication has been done.
             * @private
             */
            _showPicker: function() {
                var accessToken = gapi.auth.getToken().access_token;
                var view = new google.picker.DocsView();
                var uploadView = new google.picker.DocsUploadView();
                var viewss = new google.picker.View(google.picker.ViewId.DOCS);
                viewss.setMimeTypes('image/png,image/jpeg,image/gif,image/jpg');
                view.setIncludeFolders(true);
                this.picker = new google.picker.PickerBuilder()
                    .addView(google.picker.ViewId.DOCS_IMAGES)
                    .addView(viewss)
                    .addView(uploadView)
                    .setAppId(this.clientId)
                    .setDeveloperKey(this.apiKey)
                    .setOAuthToken(accessToken)
                    .setCallback(this._pickerCallback.bind(this))
                    .build()
                    .setVisible(true);
            },

            /**
             * Called when a file has been selected in the Google Drive file picker.
             * @private
             */
            _pickerCallback: function(data) {
                if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
                    var fruits, fLen, i;

                    fruits = data[google.picker.Response.DOCUMENTS];
                    fLen = fruits.length;
                    for (i = 0; i < fLen; i++) {

                        var file = fruits[i];
                        var id = file[google.picker.Document.ID];
                        var request = gapi.client.drive.files.get({
                            fileId: id
                        });

                        request.execute(this._fileGetCallback.bind(this));
                    }
                }
            },
            /**
             * Called when file details have been retrieved from Google Drive.
             * @private
             */
            _fileGetCallback: function(file) {
                if (this.onSelect) {
                    this.onSelect(file);
                }
            },

            /**
             * Called when the Google Drive file picker API has finished loading.
             * @private
             */
            _pickerApiLoaded: function() {
                this.buttonEl.disabled = false;
            },

            /**
             * Called when the Google Drive API has finished loading.
             * @private
             */
            _driveApiLoaded: function() {
                this._doAuth(true);
            },

            /**
             * Authenticate with Google Drive via the Google JavaScript API.
             * @private
             */
            _doAuth: function(immediate, callback) {
                gapi.auth.authorize({
                    client_id: this.clientId,
                    scope: 'https://www.googleapis.com/auth/drive',
                    immediate: immediate
                }, callback);
            }

        };
    }());
    //]]>
</script>
<script>
        function initPicker() {

            var picker = new FilePicker({
                apiKey: 'HAHAblaBlaWTFashjahsgtP6BwWPf3Liukk',
                clientId: '978946248407-WTFhahaBlaBla4q0f5c.apps.googleusercontent.com',
                buttonEl: document.getElementById('pick_image'),
                onSelect: function(file) {                  
console.log(JSON.stringify(file)); 
console.log(file); 
console.log(file.title);
console.log(file.webContentLink);
$('#post-editor').append('<a  href="'+file.webContentLink+'" class="center_img"><img src="'+file.webContentLink+'"/></a>');     
                }
            });
        }
</script>

<script src='https://www.google.com/jsapi?key=HAHAblaBlaWTFashjahsgtP6BwWPf3Liukk'></script>

<script src='https://apis.google.com/js/client.js?onload=initPicker'></script>

<button id='pick_image'>CLICK HERE INSERT OR UPLOAD IMAGE</button>
<!-- Image HTML will append to #post-editor below -->

<div id='post-editor' contenteditable='true' style='width:100%; height:200px'></div>

【问题讨论】:

    标签: javascript jquery google-api google-oauth google-picker


    【解决方案1】:

    好吧,我认为您需要为文件设置Permissions。你需要设置的是roletype

    这是文档中的示例代码。

    function insertPermission(fileId, value, type, role) {
    var body = {
    'value': value,
    'type': type,
    'role': role
    };
    var request = gapi.client.drive.permissions.insert({
    'fileId': fileId,
    'resource': body
    });
    request.execute(function(resp) { });
    }
    

    有关更多信息,请查看以下相关 SO 问题:

    【讨论】:

    • 如何用我当前的代码实现它。 插入时更新权限
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多