使用Google的apiclient获取有关用户驱动器的信息

时间:2017-06-19 05:54:51

标签: python-2.7 get google-drive-api google-api-client drive

我正在尝试使用Google的APICLIENT来访问HERE所描述的资源。即:有关用户,用户的驱动器和系统功能的信息

如果寻求驱动器元数据,客户端没有问题,但我无法在线找到有关如何使用客户端执行此身份验证版本的任何资源:

viewDidLoad

有没有人对此有任何诀窍?我可以使用一些提示。

1 个答案:

答案 0 :(得分:0)

您可以按原样申请,就像您要求在Drive API中列出或创建文件一样。你可以遵循以下两种方法之一:

v2

/**
 * Print information about the current user along with the Drive API
 * settings.
 */
function printAbout() {
  var request = gapi.client.drive.about.get();
  request.execute(function(resp) {
    console.log('Current user name: ' + resp.name);
    console.log('Root folder ID: ' + resp.rootFolderId);
    console.log('Total quota (bytes): ' + resp.quotaBytesTotal);
    console.log('Used quota (bytes): ' + resp.quotaBytesUsed);
  });
}

v3

<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 = 'CLIENT_ID';

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

      /**
       * 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', 'v3', getDetails);
      }

      function getDetails(){
        var request = gapi.client.drive.about.get({
          'fields': "user, storageQuota"
        });
        request.execute(function(resp) {
          console.log(resp)
        });

      }

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

唯一的区别是,在v3中,它要求您添加一个参数fields,该参数只会返回参数中包含的字段,例如user - 经过身份验证的用户,或者storageQuota - 用户的存储配额限制和使用情况。所有字段均以字节为单位。在v2中,它返回about方法中的所有字段。

希望它有所帮助。