How to use Google Sheets API with google service account on Node.js

You have to install googleapis package:

npm install googleapis

Save credentials for service account to the root derictory of the project under the name credentials.json

Grant access to your spreadsheet to service account.

Create index.js file and paste following code:

let {google} = require('googleapis');
let privatekey = require('./credentials.json');

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
async function getAuthToken() {
  const auth = new google.auth.GoogleAuth({
    keyFile:'./credentials.json',
    scopes: SCOPES
  });
  const authToken = await auth.getClient();
  return authToken;
}

const myId = 'YOUR SPREADSHEET ID';

const main = async () => {
  const auth = await getAuthToken()
  const client = google.sheets({ version: "v4", "auth": auth });
  const data = await client.spreadsheets.get({ spreadsheetId: myId});
  console.log(data.data)
}
main()

Run node index.js

Enjoy!

Our Visitor

0 0 0 5 5 2
Users Today : 1
Users Yesterday : 0
Users Last 7 days : 1
Users Last 30 days : 9
Users This Month : 8
Users This Year : 35
Total Users : 552
Views Today : 2
Views Yesterday :
Views Last 7 days : 2
Views Last 30 days : 10
Views This Month : 9
Views This Year : 46
Total views : 803
Who's Online : 0
Your IP Address : 3.229.123.80
Server Time : 2024-03-29

Leave a Reply

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