Google Drive API身份验证,JavaScript [英] Google Drive API authentication, JavaScript

查看:85
本文介绍了Google Drive API身份验证,JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在虚拟主机(以及Python SimpleHTTPServer)上运行,并且一直在遵循Google的说明,但是我无法获得Google驱动器授权以在页面刷新时弹出.

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/网络客户端

我已经在我的index.html文件中放置了第一段代码,并且我已经在<body>中的<script>标记中放置了以下2个代码段.我用在Google开发人员控制台中创建的ID填写了CLIENT_ID ...我还缺少什么?

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>

推荐答案

通过设置immediate=true,您将禁止auth弹出窗口.

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

第一次应该是false,之后每次每小时刷新通常设置为true.

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

这篇关于Google Drive API身份验证,JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆