I'm looking to read google spreadsheet data into a separate website, trouble with authentication or reading

Viewed 20

I have been trying to integrate an existing google spreadsheet that I would be able to pull data off of onto a separate hrml website built for mobile use. Google has just depreciated the original way of authenticating so, the tutorials I have found are old news. I have found a source of an example I am playing with so far here: "https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get"

I have an api key setup, along with an OAuth 2.0 Client ID through the google cloud website. I have python running so my http://localhost:8000 shows the website as is.

When running my program in chrome the console gives a 401 error, "m=credential_button_library:131 POST https://play.google.com/log?format=json&hasfast=true&authuser=0 401"

According to "https://developers.google.com/explorer-help/code-samples#javascript" a 401 error could be a few things to due with authentication.

Can anyone point me in the right direction to at least get a piece of my spreadsheet through to my website?

Here is what I have come up with so far:

<html>
  <head></head>
  <body>
    <script>
    function makeApiCall() {
      var params = {
        // The ID of the spreadsheet to retrieve data from.
        spreadsheetId: '1XzLr1ijIxvzeC4GKiwEZ0mY4uKT-QWmaS1GQZeqs-04', // TODO: Update placeholder value.

        // The A1 notation of the values to retrieve.
        range: 'A4', // 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 = 'AIzaSyC-ob2LWd7xN_NYGcwBmGZs9YoOOIP8nQo'; // TODO: Update placeholder with desired API key.

      var CLIENT_ID = '933256634663-ghtoihrvikgl42jl2fgk4uifb1vuedd9.apps.googleusercontent.com'; // 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 = 'https://www.googleapis.com/auth/spreadsheets';

      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();
      }
    }
    </script>
    <script src="https://accounts.google.com/gsi/client"; async defer>
        makeApiCall();</script>
    <div id="g_id_onload"
        data-client_id="933256634663-ghtoihrvikgl42jl2fgk4uifb1vuedd9.apps.googleusercontent.com">
    </div>
    <div class="g_id_signin" data-type="standard"></div>
  </body>
</html>
0 Answers
Related