-->

Google Drive API authentication, JavaScript

2019-09-07 11:00发布

问题:

I am running on a virtual host (as well as a Python SimpleHTTPServer) and I have been following Google's instructions, but I can't get the Google drive authorization to pop up on page refresh.

https://developers.google.com/drive/web/auth/web-client

Ive placed the first block of code in my index.html file, and Ive placed the following 2 snippets in <script> tags within <body>. I filled in the CLIENT_ID with the ID I created in the Google developer console...what am I missing?

 <html>
      <head>
        <script type="text/javascript">
          var CLIENT_ID = '1052173400541-355uhjrflurk7fmlon0r5umnn12i9ag3.apps.googleusercontent.com';
          var SCOPES = [
              'https://www.googleapis.com/auth/drive.file',
              'email',
              'profile',
              // Add other scopes needed by your application.
            ];

          /**
           * Called when the client library is loaded.
           */
          function handleClientLoad() {
            checkAuth();
          }

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

          /**
           * Called when authorization server replies.
           *
           * @param {Object} authResult Authorization result.
           */
          function handleAuthResult(authResult) {
            if (authResult) {
              // Access token has been successfully retrieved, requests can be sent to the API
            } else {
              // No access token could be retrieved, force the authorization flow.
              gapi.auth.authorize(
                  {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
                  handleAuthResult);
            }
          }
        </script>
        <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
      </head>
      <body>
        <h1>welcome to google_drive.local</h1>
        <script type='text/javascript'>
            /**
            * Load the Drive API client.
             * @param {Function} callback Function to call when the client is loaded.
             */
            function loadClient(callback) {
              gapi.client.load('drive', 'v2', callback);
            }

            /**
     * Print a file's metadata.
     *
     * @param {String} fileId ID of the file to print metadata for.
     */
            function printFile(fileId) {
              var request = gapi.client.drive.files.get({
                  'fileId': fileId
              });
              request.execute(function(resp) {
                if (!resp.error) {
                  console.log('Title: ' + resp.title);
                  console.log('Description: ' + resp.description);
                  console.log('MIME type: ' + resp.mimeType);
                } else if (resp.error.code == 401) {
                  // Access token might have expired.
                  checkAuth();
                } else {
                  console.log('An error occured: ' + resp.error.message);
                }
              });
            }
        </script>
      </body>
    </html>

回答1:

By setting immediate=true you are suppressing the auth popup.

It should be false the first time through and is generally set to true thereafter for each hourly refresh.