我嘗試使用Google Drive Picker API上傳新圖片或將圖片插入我的CMS。如何在使用Javascript插入文件時更改Google Drive文件的可訪問性Google Picker API
它的工作。我可以獲取圖片網址。但問題是:默認情況下,上傳到Google雲端硬盤的文件的訪問權限是私密的(只有我作爲所有者才能通過登錄Google帳戶查看)。但是,當我手動將文件的輔助功能設置更改爲Public或來自Google雲端硬盤站點的鏈接的任何人時,瀏覽器上的任何人都可以看到此圖像。
我在這裏指出:如何改變Google Drive文件的可訪問性,使用Google Picker javascript API插入文件(更改/設置公開或任何鏈接的可訪問性)。以下是我目前的代碼。
<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'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>
如何與我當前的代碼實現它。更新插入**時的權限** –