I have been trying to get Google Cloud Functions to obtain the default service account credentials automatically with getClient() but when I use it the function is unresponsive and eventually times out. I am using the Google Sheets API just to obtain a value for now.
I have made sure to grant the service account the right access, share the sheets with the service email, checked my dependencies.
I have seen a few answers and guides (here and here) using getClient but I don't seem to be able to get it to work, here is my code:
const {google} = require('googleapis');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const PORT = process.env.PORT || 8080;
const SHEET_ID = "1ie8......lvnaKM";
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
app.get('/', (req, res) => {
let range = 'test2!B1';
getSheetsValue(SHEET_ID, range, res);
});
app.listen(PORT, () => { });
exports.sheetsTest = app;
async function getSheetsValue(sheetID, range, res) {
const authClient = await google.auth.getClient({
scopes: SCOPES,
});
console.log(authClient);
const sheets = google.sheets({
version: 'v4',
authClient
});
request = {
spreadsheetId: sheetID,
range: range,
};
try {
const result = await sheets.spreadsheets.values.get(request);
console.log(`result: ${JSON.stringify(result)}`);
console.log(`result.data: ${JSON.stringify(result.data)}`);
res.json(result.data.values[0]);
} catch (e) {
return e;
}
}
I've even tried it with the google-auth-library per instructions here but it didn't work: https://github.com/googleapis/google-auth-library-nodejs#application-default-credentials
Until then hard coding the credentials into the program works, I was just hoping to figure out why this does not work. Ideally I'd like use the implicit credentials
Here is the code that works on Google Cloud Functions for me:
const {google} = require('googleapis');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const PORT = process.env.PORT || 8080;
const SHEET_ID = "SHEET_ID";
const CLIENT_EMAIL = "SERVICE_ACCOUNT_EMAIL";
const PRIVATE_KEY = "KEY"; //Credentials from the JSON file after downloading it
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
app.get('/', (req, res) => {
let range = 'test2!B1';
getValue(SHEET_ID, range, res);
});
app.listen(PORT, () => { });
exports.sheetsTest = app;
async function getValue(sheetID, range, res) {
let authClient = new google.auth.JWT(CLIENT_EMAIL, null, PRIVATE_KEY, SCOPES);
const sheets = google.sheets({
version: 'v4',
auth: authClient,
});
request = {
spreadsheetId: sheetID,
range: range,
}
try {
const result = await sheets.spreadsheets.values.get(request);
res.json(result.data.values[0]);
} catch (e) {
return e;
}
}
Thank you