I am trying to write to a Google Spreadsheet from an Actions on Google app with Dialogflow. I am able to read from the sheet using my app. The spreadsheet is given a public edit access.
I am using below code in the fulfillment webhook to read and write to spreadsheet. I have my SPREADSHEET_ID and SPREADSHEET_API_KEY:
function welcome(agent) {
const tabName = 'Sheet1';
const startCell = 'B2';
const endCell = 'D';
appendDataToSpreadsheet(tabName, startCell, endCell);
agent.add(`Appended`);
}
function appendDataToSpreadsheet(tabName, startCell, endCell) {
const sheets = google.sheets({version: 'v4', auth: SPREADSHEET_API_KEY});
return sheets.spreadsheets.values.append({
auth: auth,
spreadsheetId: SPREADSHEET_ID,
range: `${tabName}!${startCell}:${endCell}`,
valueInputOption: "USER_ENTERED",
resource: {
values: [ ["5", "Anis", "8", "React"], ["6", "Paul", "1", "Python"] ]
}
}, (err, response) => {
if (err) {
console.log('The API returned an error: ' + err);
return;
} else {
console.log("Appended");
}
});
}
This code does not append the data to spreadsheet and in my logs I can see following error:
The API returned an error:
Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. Seehttps://developers.google.com/identity/sign-in/web/devconsole-project
.
The app is asking for authentication credential. Any pointers on how to go about this will be helpful.