1

I'm trying to use the Google Drive File Picker as a file uploader for my site. I've got most everything working, but I've been stuck trying to get the image URL for images uploaded via the picker. Here's the JS I have now:

/******************** 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;
}

As you can see in the pickerCallback function, it has selected the largest image in the JSON data it returned. However, this image is only 512px wide, when the image I uploaded is much, much larger. The photo shows up at full resolution in my Google Photos library, but I can't find a way to get the high res version programmatically. Here's the JSON data that the picker returns after uploading:

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

A couple more things, the 'url' part of the data links to a larger, but still not full resolution version of the picture on Picasa Web Albums. Is this a reliable source for the image url? And if so, how could I extract the url from there?

Here is a live demo of what I have so far.

4

2 回答 2

3

Use DocsView() in your function createPicker to get the drive url not a picasa url. Then open the returned url with your access token.

Here is a complete working example with some extra optional settings

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

This will return a url samilar to this https:// drive.google.com/file/d/file_id/view?usp=drive_web Which you can download using a GET request to google https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents

If you want to download it client side. You can set your callback function to make a blob GET call using the returned file id and access token as below.

  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();
  }
于 2017-11-30T13:08:59.033 回答
1

You will not get a image url that is directly accessible to get your image file, however you can get your original image by using the id returned by picker api as follows

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer [Your access token]

Use this to get the content of your file and display it as image using the mimeType returned

Google Drive API guide where this is described

于 2016-06-26T02:39:07.343 回答