2015-04-14 126 views
0

我在虛擬主機(以及Python SimpleHTTPServer)上運行,並且一直遵循Google的說明,但我無法獲得Google驅動器授權以在刷新頁面時彈出。Google Drive API身份驗證,JavaScript

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

伊夫放置的代碼的第一個塊中我index.html文件,並且Ive放入下列2段中<script>標籤內<body>。我用我在Google開發者控制檯中創建的ID填寫了CLIENT_ID ......我錯過了什麼?

<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> 

回答

2

通過設置immediate=true你抑制AUTH彈出。

第一次應該是false,之後每次每小時刷新一般設置爲true