Access Google Sheets from Google Charts with OAuth

I wrote the procedure to read Google Sheets from Google Charts on this page before. Anyone can read the Sheet data with this method.

Google Charts document describes the method to read Google Sheets with authentication on this page. I tested the method with OAuth this time.

I tested the method, “Example: Using OAuth to access /gviz/tq”.

First, I prepared “OAuth client ID” by following the procedure described below.

1. From the developer console, create a new OAuth client ID.
2. Choose Web application as your application type.
3. Pick any name; it is for your information only.
4. Add the name of your domain (and any test domains) as Authorized JavaScript Origins.
5. Leave Authorized redirect URIs blank.

Next, I wrote the following JavaScript by referring to the sample codes described here. The code plotted the graph with Google Sheets data which allow the read access to the limited users.

# In the following JavaScript, we need to use valid clientId and tpUrl that is suitable for our environment.

<button id="authorize-button" style="visibility: hidden">Authorize</button>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
google.charts.load('current', {packages:['corechart']});

const clientId = '';
const scopes = '';

function init() {
        {client_id: clientId, scope: scopes, immediate: true},

function handleAuthResult(authResult) {
    const authorizeButton = document.getElementById('authorize-button');
    if (authResult && !authResult.error) { = 'hidden';
    } else { = '';
        authorizeButton.onclick = handleAuthClick;

function handleAuthClick(event) {
        {client_id: clientId, scope: scopes, immediate: false},
    return false;

function makeApiCall() {
    const tqUrl = ''
        + '?access_token=' + encodeURIComponent(gapi.auth.getToken().access_token);
    const query = new google.visualization.Query(tqUrl);

const skip_length = 1;
const time_interval = 0.01;

function drawChart(response) {
    const data = response.getDataTable();
    const numRows = data.getNumberOfRows();
    const numThinnedRows = Math.floor(numRows / skip_length);
    const remainder = numRows % skip_length;

    for (let rowIndex = 0; rowIndex < numThinnedRows; rowIndex++) {
        data.removeRows(rowIndex, skip_length - 1);

    if (remainder != 0) {
        data.removeRows(numThinnedRows, remainder);

    // data thinning
    const options = {title: 'phyphox 3-axis acceleration sensor data (after data thinning)',
                     hAxis: {title: 'time[s]'},
                     vAxis: {title: 'acceleration [m/s^2]'}};
    const chart = new google.visualization.LineChart(document.getElementById('after_data_thinning'));
    chart.draw(data, options);
    // ...

<script src=""></script>

I prepared Web Application whose public status is “Test” on OAuth page in Google Cloud Platform. I registered a test user on this Web Application. I also specified the domain name ( which refers to the Google Sheets.

When I wrote the JavaScript on my web page, the “Authorize” button was displayed.

When I clicked the “Authorize” button with Chrome, the window below was displayed.

The following window was displayed when I clicked the “Authorize” button with Firefox.

Google Charts graphs were displayed when I entered the email address of the registered test user. (We would be required to enter our valid passwords if we are not using Chrome with the registered user’s account.)

The graph would be displayed at the place where we write the following code (in case of the above JavaScript).

<div id="after_data_thinning" style="width: 100%; height: 500px;"></div>

The window below was displayed when I entered the unregistered email address.

Leave a Reply

Your email address will not be published. Required fields are marked *