See Thumbnails with the Google Drive API

4

How can I see the thumbnails of my images from google Drive ?

That is to say, that the api of google drive returns the url of the thumbnail. I also use jquery , if necessary or to facilitate it.

<html>
  <head>
    <script type="text/javascript">
      // Your Client ID can be retrieved from your project in the Google
      // Developer Console, https://console.developers.google.com
      var CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com';

      var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];

      /**
       * Check if current user has authorized this application.
       */
      function checkAuth() {
        gapi.auth.authorize(
          {
            'client_id': CLIENT_ID,
            'scope': SCOPES.join(' '),
            'immediate': true
          }, handleAuthResult);
      }

      /**
       * Handle response from authorization server.
       *
       * @param {Object} authResult Authorization result.
       */
      function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
          // Hide auth UI, then load client library.
          authorizeDiv.style.display = 'none';
          loadDriveApi();
        } else {
          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
          authorizeDiv.style.display = 'inline';
        }
      }

      /**
       * Initiate auth flow in response to user clicking authorize button.
       *
       * @param {Event} event Button click event.
       */
      function handleAuthClick(event) {
        gapi.auth.authorize(
          {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
          handleAuthResult);
        return false;
      }

      /**
       * Load Drive API client library.
       */
      function loadDriveApi() {
        gapi.client.load('drive', 'v2', listFiles);
      }

      /**
       * Print files.
       */
      function listFiles() {
        var request = gapi.client.drive.files.list({
            'maxResults': 10
          });

          request.execute(function(resp) {
            appendPre('Files:');
            var files = resp.items;
            if (files && files.length > 0) {
              for (var i = 0; i < files.length; i++) {
                var file = files[i];
                appendPre(file.title + ' (' + file.id + ')');
              }
            } else {
              appendPre('No files found.');
            }
          });
      }

      /**
       * Append a pre element to the body containing the given message
       * as its text node.
       *
       * @param {string} message Text to be placed in pre element.
       */
      function appendPre(message) {
        var pre = document.getElementById('output');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);

      }

    </script>
    <script src="https://apis.google.com/js/client.js?onload=checkAuth">
    </script>
  </head>
  <body>
    <div id="authorize-div" style="display: none">
      <span>Authorize access to Drive API</span>
      <!--Button for the user to click to initiate auth sequence -->
      <button id="authorize-button" onclick="handleAuthClick(event)">
        Authorize
      </button>
    </div>
    <pre id="output"></pre>
  </body>
</html>
    
asked by Alejandro Acu 21.12.2015 в 19:45
source

1 answer

1

According to the documentation , it looks like there should be some options available to get the thumbnail of a file in Google Drive:

  • thumbnailLink - A text string with a temporary link (only lasts a few hours) at the thumbnail file.

  • thumbnail.image : the bytes of the thumbnail of the file encoded in Base-64 (according to section 5 of the RFC 4648 )

  • ... but either there is some kind of bug, or I have not been able to use them (much more possible), because it seems that they do not work at all and they always return undefined .

    If you can not get the above methods to work for you, you could use a simple alternative: use the URL that Google Drive uses to display the thumbnails : link .

    So, for example, in the code above to get the URL of thumbnail you could do something like this:

    request.execute(function(resp) {
        appendPre('Files:');
        var files = resp.items;
        if (files && files.length > 0) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                appendPre(file.title + ' (' + file.id + ')');
    
    
                // Código para el thumbnail
                var thumbnail = "https://drive.google.com/thumbnail?id=" + file.id;
                appendPre("THUMBNAIL = " + thumbnail);
            }
        } else {
            appendPre('No files found.');
        }
    });
    
        
    answered by 23.12.2015 в 00:20