Tips on extracting the image URL after uploading via Google Picker

I'm currently implementing the Google Drive File Picker on my website for file uploading. Everything seems to be working well, except I am facing an issue with retrieving the image URL for images uploaded through the picker. Below is my current JavaScript code:

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

In the pickerCallback function, it grabs the largest image in the JSON data. However, this image size is only 512px wide while the uploaded image resolution is higher. The full-resolution photo can be seen in my Google Photos library but programmatically extracting the high-res version poses a challenge. Below is the JSON data obtained 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

Moreover, the 'url' component of the data links to a larger yet not full-resolution picture on Picasa Web Albums. Is this link considered reliable for obtaining the image URL? If so, how can I extract the URL from there?

Here is a live demo showcasing the progress made.

Answer №1

To ensure you get the drive URL instead of a Picasa URL, utilize the DocsView() function within your createPicker function. Afterwards, open the obtained URL with your access token.

Below is a comprehensive working example with some additional 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);
    }
  }

The returned URL will be similar to this format: https://drive.google.com/file/d/file_id/view?usp=drive_web. You can download it by making a GET request to Google at https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents

If you prefer to download it client-side, set up your callback function to initiate a blob GET call using the received file ID and access token as shown 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();
  }

Answer №2

To attain a direct image url for your image file is not possible, but you can still retrieve your original image by leveraging the id given by the picker API in the following manner:

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

You can utilize this method to fetch the content of your file and showcase it as an image using the mimeType provided.

Visit the Google Drive API guide for further details on this process

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Managing operations in Swift 2.0

I am currently working with Swift 2.0 on IOS 9.2.1 using Xcode 7.2. As I am learning Swift 2.0, I have developed a routine that involves creating a NSURL Session to retrieve JSON data and parsing it successfully. Everything is working as expected... Howe ...

Deleting the clone <div> while ensuring the main <div> is kept clear of any remaining data

Initially: After adding a new row and then removing it. Why is this happening? When I set val(""), the textbox should have no value. What mistake did I make in my code? Please assist. Here's the code snippet: <div id="rows" class="block"> ...

Unable to process despite clicking button (no error messages)

I'm in the process of setting up an options page for my very first extension. As a beginner in coding, I might have made some rookie mistakes along the way. However, I am facing challenges with basic functionality that was previously working seamlessl ...

javascript - The onmessage event will not trigger when using a Java server

I have encountered an issue with my Java server and JavaScript websocket client. Despite trying various solutions from this site, I am still unable to resolve the problem. So, I decided to share my code here in hopes of getting some assistance. The proble ...

Choose checkboxes based on the checkboxes that were previously selected

My goal is to have a checkbox automatically selected based on the previously selected checkbox. For example, if I select the first checkbox (which has a specific class), then only the checkbox with the same class as the first one should be selected using j ...

React powered interactive tables

I am in the process of creating a dynamic table using React, and here is the data structure I am working with: { numRows: 2, numCols: 3, cells: [ { id: 1, pos: { row: 1, col: 1 }, content: 'This is th ...

Retrieve the file that has been uploaded to AWS S3

Here is a snippet of code to consider : var express = require('express'), aws = require('aws-sdk'), bodyParser = require('body-parser'), multer = require('multer'), multerS3 = req ...

Having issues with jQuery when trying to select only a single checkbox?

I have created a table with four rows and eight columns, each containing a checkbox. My goal is to allow only one checkbox to be checked per row. I am attempting to achieve this using jQuery. While it works in jsfiddle, I am experiencing difficulties in ge ...

Learn how to render a dynamic checkbox that is connected with AJAX and PHP for seamless functionality

I need to showcase a dynamic checkbox that can be bound using ajax and php. Here is my code: <?php include 'dbconnect.php'; $result = mysqli_query($link, "SELECT * FROM city where district_id='$dist' "); while($city_row=mysqli_fe ...

By default, Nuxt 2.15.7 is automatically installed when you create a new Nuxt app

I am looking to develop a Nuxt app (version 3) using Vue. However, when I run the command npm create nuxt-app@version mousa, it automatically installs Nuxt2. How can I install Nuxt3 instead with this command? ...

How come the use of a timeout causes the this variable to seemingly lose its reference?

What is the reason why this: myelements.mouseenter(function() { clearTimeout(globaltimeoutvar); globaltimeoutvar = setTimeout(function() { var index = myelements.index(this); console.log(index); // -1 }, 150); }); Returns -1, while this: m ...

Having trouble with React's conditional rendering not working as expected?

I am currently working on updating the contents of my Navbar and Router by using useContext and conditional rendering techniques. Here is a snippet from my App.js: import "./App.css"; import axios from "axios"; import { AuthContextProv ...

I am eager to display this JSON data using AngularJS tables

I have JSON file with content about categories, departments, and digital marketing: { "categories":[ { "dept_id":"123", "category_name":"database", "category_discription":"list of database", "current time ...

Set up a Bootstrap date picker to populate two input boxes with a start and end date. Additionally, disable the ability for the date value to change

In my project, I have implemented Bootstrap Datepicker to set two input boxes for selecting start and end dates. The rule is that the start date must be after today and the end date must be after the selected start date. To disable specific dates in the da ...

A single list is utilized by multiple HTML5 selects

If I have 10 fields in a form, each requiring a select option from the year 1950 to 2017, can I create one list from that range and have each select reference it? Or do I need to make ten separate lists for each select? Edit: An example could be the birth ...

Exploring the functionalities of using Script in Next.js 13

I want to include a vanilla JavaScript script in my next.js application like this: import Script from "next/script"; <Component {...pageProps} /> <Script id="raychat-widget" strategy="afterInteractive&quo ...

Guidelines for positioning an object to float in the bottom right of a separate tag

I am trying to position an image at the bottom right of a parent div. The CSS solution I found from another answer did not work as expected, so I had to use JavaScript to achieve the desired result. Is there a way to do this without using JavaScript? (Pl ...

Generating a hierarchical JSON structure using Pandas for an organizational chart

I am working on transforming a hierarchical DataFrame in Python 3.5 into a nested JSON object for use in JavaScript to display an Org Chart. I am aiming to achieve the structure outlined in the response provided in this question: Organization chart - tree, ...

What is the best way to position a rectangle on top of a div that has been rendered using

I recently started using a waveform display/play library known as wavesurfer. Within the code snippet below, I have integrated two wavesurfer objects that are displayed and positioned inside div elements of type "container". My goal is to position my own ...

jQuery Filter for Page Content - choose specific text within paragraphs and clickable links

I recently created a page search filter using Bootstrap 5, but it seems to only display text content and not any content enclosed within the a tags. You can check out the JS Fiddle link provided below for reference: https://jsfiddle.net/mfen723/rozy16pt/1 ...