我正在尝试将Google Drive File Picker用作我网站的文件上传器。我已经掌握了大部分工作,但我一直在试图获取通过选择器上传的图像的图像URL。这是我现在的JS:
/******************** Google Picker Script ********************/
var developerKey = 'AIzaSyCCKBK236c5tH7pUjHlz485R7Xi-m64EDg', //Browser API key
clientId = '958305636628-7hvvhnprofn4thnvatdhc7pvucd2efkf.apps.googleusercontent.com', //Client ID
scope = ['https://www.googleapis.com/auth/photos','https://www.googleapis.com/auth/photos.upload','https://www.googleapis.com/auth/drive.readonly'], //Permission scope
pickerApiLoaded = false,
oauthToken;
function onApiLoad() {
// Use the API Loader script to load google.picker and gapi.auth.
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() {
// Create and render a Picker object for picking user Photos.
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
addView(google.picker.ViewId.PHOTOS).
addView(google.picker.ViewId.PHOTO_UPLOAD).
addView(google.picker.ViewId.IMAGE_SEARCH).
addView(google.picker.ViewId.VIDEO_SEARCH).
addView(google.picker.ViewId.DOCUMENTS).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
function pickerCallback(data) {
// A simple callback implementation.
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
console.log(doc);
var thumbs = data.docs[0].thumbnails;
var imageURL = thumbs[thumbs.length - 1].url; //select the largest image returned
}
var message = 'You picked: <br/><img src="'+imageURL+'"/>';
document.getElementById('result').innerHTML = message;
}
正如您在pickerCallback函数中看到的,它选择了它返回的JSON数据中的最大图像。但是,当我上传的图像更大,更大时,此图像只有512像素宽。照片在我的Google照片库中以全分辨率显示,但我找不到以编程方式获取高分辨率版本的方法。以下是上传后选择器返回的JSON数据:
Object {id: "6228330388484160754", serviceId: "picasa", mimeType: "application/vnd.google-apps.photo", name: "20151128_205613.jpg", description: ""…}
description: ""
iconUrl: "https://ssl.gstatic.com/docs/doclist/images/icon_10_generic_list.png"
id: "6228330388484160754"
lastEditedUtc: 1448762173000
mediaKey: "AF1QipMhAlAX4va1MMnOF_yNzYBPFN0eg75xQ12Cu16g"
mimeType: "application/vnd.google-apps.photo"
name: "20151128_205613.jpg"
parentId: "6228330389946890849"
rotation: 0
serviceId: "picasa"
sizeBytes: 2449784
thumbnails: Array[5]
0: Object
height: 32
url: "https://lh3.googleusercontent.com/-Ngxytft7Pv8/Vm95fT1efPI/AAAAAAAA3gE/_aUFMZAvAxw/s32-c/20151128_205613.jpg"
width: 32
1: Object
height: 64
url: "https://lh3.googleusercontent.com/-Ngxytft7Pv8/Vm95fT1efPI/AAAAAAAA3gE/_aUFMZAvAxw/s64-c/20151128_205613.jpg"
width: 64
2: Object
height: 72
url: "https://lh3.googleusercontent.com/-Ngxytft7Pv8/Vm95fT1efPI/AAAAAAAA3gE/_aUFMZAvAxw/s72-c/20151128_205613.jpg"
width: 72
3: Object
height: 225
url: "https://lh3.googleusercontent.com/-Ngxytft7Pv8/Vm95fT1efPI/AAAAAAAA3gE/_aUFMZAvAxw/s400/20151128_205613.jpg"
width: 400
4: Object
height: 288
url: "https://lh3.googleusercontent.com/-Ngxytft7Pv8/Vm95fT1efPI/AAAAAAAA3gE/_aUFMZAvAxw/20151128_205613.jpg"
width: 512
length: 5
type: "photo"
url: "https://picasaweb.google.com/101379568166574033142/20151215#6228330388484160754"
version: 56833
还有一些事情,数据的“网址”部分链接到Picasa网络相册上图片的较大但仍未完整分辨率版本。这是图像网址的可靠来源吗?如果是这样,我怎么能从那里提取网址?
这是我目前所拥有的live demo。
答案 0 :(得分:2)
在您的函数 createPicker 中使用 DocsView(),以获取驱动器网址而非picasa网址。然后使用您的访问令牌打开返回的URL。
以下是一个完整的工作示例,其中包含一些额外的可选设置
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var view = new google.picker.DocsView();
//start on drive root and include folders to avoid the default flat file list
view.setParent('root');//optional
view.setIncludeFolders(true);//optional
var picker = new google.picker.PickerBuilder().
addView(view).
addView(google.picker.ViewId.DOCS).
enableFeature(google.picker.Feature.MULTISELECT_ENABLED).//optional
enableFeature(google.picker.Feature.NAV_HIDDEN).//optional
setOAuthToken(oauthToken).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
这将返回一个url samilar到此 https:// drive.google.com/file/d/file_id/view?usp=drive_web 您可以使用GET请求下载google { {3}}
如果要下载客户端。您可以设置回调函数,使用返回的文件ID和访问令牌进行blob GET调用,如下所示。
function pickerCallback(data) {
var id = data[google.picker.Response.DOCUMENTS][0].id;//can be a loop if multiselect is enabled
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
var url = "https://www.googleapis.com/drive/v3/files/" + id + "?alt=media";
xhr.open('GET', url);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.responseType = "blob";
xhr.addEventListener('load', function(e) {
var blob = this.response;//this is your blob file
});
xhr.send();
}
答案 1 :(得分:1)
您将无法获得可直接访问图像文件的图片网址,但您可以使用picker api返回的ID获取原始图片,如下所示
GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer [Your access token]
使用此选项获取文件内容并使用返回的mimeType将其显示为图像