Use gapi.client JavaScript to show data on a page

78 views Asked by At

I'm new to Google Docs API. and seem to be running in to may issue following examples. Each example seems to have its own point of failure. I'm also not able to find a question I need answered. Can anyone provide me with two answers?

  1. Can I set up a page to retrieve data from a Google spread sheets. In so that, the end user does not need any credentials. AS the developer I'd like to hide that. I just need the page to shows the data?

  2. Is there any example that will do this with a key and client id and sheet ID or URL? If not what more do I need?

Example
If I use this code, I get an uncaught errors with a huge blob of code @ cb=gapi.loaded_0:469:246

function start() {
  // 2. Initialize the JavaScript client library.
  gapi.client.init({
    'apiKey': API_KEY,
    'discoveryDocs': ["https://sheets.googleapis.com/$discovery/rest?version=v4"],
    'clientId': CLIENT_ID,
    'scope': 'profile',
  }).then(function() {
    return gapi.client.sheets.spreadsheets.values.get({
      spreadsheetId: '1OwKALJeMkprkbY68iGrCvFUhU4ZascoDLaw34fzeYaA',
      range: 'C506 3mm!A1:B6', 
    })
  }).then(function(response) {
    console.log(response.result);
  }, function(reason) {
    console.log(reason);
  });
};

function gapiLoaded() {
  // Loads the JavaScript client library and invokes `start` afterwards.
    gapi.load('client', start);
}
 

update I found out I can not do this from a local PC, so I moved to a web server. I also had to change my type from desktop to web page. So I change my code some.

function start() {
  // 2. Initialize the JavaScript client library.

  gapi.client.init({
    'apiKey': API_KEY,
    // clientId and scope are optional if auth is not required.
    'clientId': CLIENT_ID,
    'discoveryDocs': ["https://sheets.googleapis.com/$discovery/rest?version=v4"],
    'scope': 'email',
  }).then(function() {
    return gapi.client.sheets.spreadsheets.values.get({
      spreadsheetId: '1OwKALJeMkprkbY68iGrCvFUhU4ZascoDLaw34fzeYaA',
      range: 'C506 3mm!A1:B6', 
    })
  }).then(function(response) {
    console.log(response.result);
  }, function(reason) {
    console.log(reason);
  });
};

function gapiLoaded() {
    gapi.load('client:auth2', start);
}

but I'm getting the migration error. details:

"You have created a new client application that uses libraries for user authentication or authorization that are deprecated. New clients must use the new libraries instead. See the Migration Guide for more information."
error:
"idpiframe_initialization_failed"

I tried something like this with the new API:

window.onload = function () 
    {
        client = google.accounts.oauth2.initTokenClient({
              client_id: CLIENT_ID,
              scope: 'https://www.googleapis.com/auth/spreadsheets.readonly',
              callback: (tokenResponse) => {
                access_token = tokenResponse.access_token;
              },
        })
    }

and the call back never fires.

0

There are 0 answers