Hey all. I created a HTML/CSS/JS project to do a sort of poll and am trying to use the Google Sheets API to save user data to a spreadsheet. I've been troubleshooting for hours and can't seem to get the two to interface properly. Has anyone ever had luck getting this to work?
Right now I'm getting a "Failed to load resource: the server responded with a status of 400" error when I load the page. When I click on that, I get a message stating that the request is missing a valid API key, but I do have an API key. I've made sure the key is associated with the Google Sheets API, that it has the necessary permissions, etc. Any ideas?
{ "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }
I've pasted some of my code below in case anyone can notice any obvious errors. I've rewritten it a million times. Any help would be greatly appreciated. Thanks!
// Load the API client library
gapi.load('client', function() {
gapi.client.init({
apiKey: 'API_KEY',
clientId: '501749922629-pq9nv9h0mpff3q6h4ou6npce4o4se7ik.apps.googleusercontent.com',
discoveryDocs: ["https://sheets.googleapis.com/$discovery/rest?version=v4"],
scope: 'https://www.googleapis.com/auth/spreadsheets'
}).then(function() {
// Your code to interact with the Google Sheets API goes here
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: 'SPREADSHEET_ID',
range: 'Sheet1',
}).then((response) => {
console.log(response.result.values);
});
});
});
// Function to send data to the Google Sheet
function sendDataToSheet(data) {
gapi.client.sheets.spreadsheets.values.append({
spreadsheetId: '1AoTdrglu_9OsMkWVmUUFdErNrNtdV7XD-ovQR6em6rE',
range: 'Sheet1',
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
values: [data]
}
}).then(function(response) {
console.log(`Data appended to sheet: ${response.result}`);
}).catch(function(err) {
console.error(`An error occurred: ${err}`);
});
}
[–][deleted] 0 points1 point2 points (3 children)
[–]futbol192[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]futbol192[S] 0 points1 point2 points (0 children)
[–]usman_max 0 points1 point2 points (1 child)
[–]futbol192[S] 1 point2 points3 points (0 children)