首页 > 解决方案 > 在客户端选择没有 OATH 的带有 Javascript 的特定数据 Google 表格

问题描述

我有一个<iframe>需要动态和远程更改的“url”。我不想在中定义“src”,<iframe>而是通过变量传递它。

因此,我创建了一个包含两个单元格的 Google 表格文件:A1 是标题(“URL”),B1 是实际的 url(例如https://youtube.com/...),将不时手动更改为时间。

我激活了 Google Sheets API。

这是 JSON:

{
  "range": "'RangeTitle'!A1:Z1000",
  "majorDimension": "ROWS",
  "values": [
    [
      "URL",
      "https://www.youtube.com/..."
    ]
  ]
}

这是它提供的示例代码:

<html>
  <head></head>
  <body>
    <!--
    BEFORE RUNNING:
    ---------------
    1. If not already done, enable the Google Sheets API
       and check the quota for your project at
       https://console.developers.google.com/apis/api/sheets
    2. Get access keys for your application. See
       https://developers.google.com/api-client-library/javascript/start/start-js#get-access-keys-for-your-application
    3. For additional information on authentication, see
       https://developers.google.com/sheets/api/quickstart/js#step_2_set_up_the_sample
    -->
    <script>
    function makeApiCall() {
      var params = {
        // The ID of the spreadsheet to retrieve data from.
        spreadsheetId: 'my-spreadsheet-id',  // TODO: Update placeholder value.

        // The A1 notation of the values to retrieve.
        range: 'my-range',  // TODO: Update placeholder value.

        // How values should be represented in the output.
        // The default render option is ValueRenderOption.FORMATTED_VALUE.
        valueRenderOption: '',  // TODO: Update placeholder value.

        // How dates, times, and durations should be represented in the output.
        // This is ignored if value_render_option is
        // FORMATTED_VALUE.
        // The default dateTime render option is [DateTimeRenderOption.SERIAL_NUMBER].
        dateTimeRenderOption: '',  // TODO: Update placeholder value.
      };

      var request = gapi.client.sheets.spreadsheets.values.get(params);
      request.then(function(response) {
        // TODO: Change code below to process the `response` object:
        console.log(response.result);
      }, function(reason) {
        console.error('error: ' + reason.result.error.message);
      });
    }

    function initClient() {
      var API_KEY = '';  // TODO: Update placeholder with desired API key.

      var CLIENT_ID = '';  // TODO: Update placeholder with desired client ID.

      // TODO: Authorize using one of the following scopes:
      //   'https://www.googleapis.com/auth/drive'
      //   'https://www.googleapis.com/auth/drive.file'
      //   'https://www.googleapis.com/auth/drive.readonly'
      //   'https://www.googleapis.com/auth/spreadsheets'
      //   'https://www.googleapis.com/auth/spreadsheets.readonly'
      var SCOPE = '';

      gapi.client.init({
        'apiKey': API_KEY,
        'clientId': CLIENT_ID,
        'scope': SCOPE,
        'discoveryDocs': ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
      }).then(function() {
        gapi.auth2.getAuthInstance().isSignedIn.listen(updateSignInStatus);
        updateSignInStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
      });
    }

    function handleClientLoad() {
      gapi.load('client:auth2', initClient);
    }

    function updateSignInStatus(isSignedIn) {
      if (isSignedIn) {
        makeApiCall();
      }
    }

    function handleSignInClick(event) {
      gapi.auth2.getAuthInstance().signIn();
    }

    function handleSignOutClick(event) {
      gapi.auth2.getAuthInstance().signOut();
    }
    </script>
    <script async defer src="https://apis.google.com/js/api.js"
      onload="this.onload=function(){};handleClientLoad()"
      onreadystatechange="if (this.readyState === 'complete') this.onload()">
    </script>
    <button id="signin-button" onclick="handleSignInClick()">Sign in</button>
    <button id="signout-button" onclick="handleSignOutClick()">Sign out</button>
  </body>
</html>

我填写了必填字段。但是,最终,它需要用户登录才能读取数据。因此我想问你:

  1. 如果有一种方法可以在没有 OATH 登录的情况下从用户端读取 Google Sheets API 数据。
  2. 如何将 URL 从 B1 单元格传递<iframe src="">给变量。

最后,我愿意接受另一种实现相同结果的方法(<iframe src="">从外部源动态地将 url 传递给 )。

标签: javascriptiframegoogle-sheets-api

解决方案


我了解到您想在不从用户帐户登录的情况下使用 Sheets API。如果这是正确的,那么您可以创建一个服务帐户来实现您的目标。然后,您可以从管理控制台将域范围的权限委派给服务帐户。通过这种方式,您只需从服务帐户登录 OAuth 2.0 并使用它来访问用户的数据。请问我任何其他疑问。


推荐阅读