googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.27k stars 1.91k forks source link

Sheets API isn't appending new values #3389

Closed aes219 closed 7 months ago

aes219 commented 7 months ago

I'm using googleapis in a backend server, some certain endpoints of the server work and googleapis does save items in sheets, but this specific endpoint ain't working. It does print "Done" as per line 30 but doesn't really append any values in the sheet. The path to the credentials file is absolutely correct and is being used by the other endpoints that work, the range is also correct with the right sheet name. When logging response in the console as per line 32, it returns the following :

{"config":{"url":"https://sheets.googleapis.com/v4/spreadsheets/{sheetid}/values/Profiles%21A%3AZ:append?valueInputOption=USER_ENTERED","method":"POST","userAgentDirectives":[{"product":"google-api-nodejs-client","version":"7.0.0","comment":"gzip"}],"data":{"values":[[]]},"headers":{"x-goog-api-client":"gdcl/7.0.0 gl-node/18.18.0","Accept-Encoding":"gzip","User-Agent":"google-api-nodejs-client/7.0.0 (gzip)","Authorization":"Bearer {token}","Content-Type":"application/json"},"params":{"valueInputOption":"USER_ENTERED"},"retry":true,"body":"{\"values\":[[]]}","responseType":"unknown"},"data":{"spreadsheetId":"{sheetid}","tableRange":"Profiles!A1:E2","updates":{"spreadsheetId":"{sheetid}","updatedRange":"Profiles!A3"}},"headers":{"alt-svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000","cache-control":"private","connection":"close","content-encoding":"gzip","content-type":"application/json; charset=UTF-8","date":"Fri, 17 Nov 2023 13:01:53 GMT","server":"ESF","transfer-encoding":"chunked","vary":"Origin, X-Origin, Referer","x-content-type-options":"nosniff","x-frame-options":"SAMEORIGIN","x-l2-request-path":"l2-managed-6","x-xss-protection":"0"},"status":200,"statusText":"OK","request":{"responseURL":"https://sheets.googleapis.com/v4/spreadsheets/{sheetid}/values/Profiles%21A%3AZ:append?valueInputOption=USER_ENTERED"}}

Replaced the bearer token and sheet id with {token} & {sheetid} in above output

Here is the backend server code :

require('dotenv').config()
const { google } = require('googleapis');
const sheets = google.sheets({ version: 'v4' })
const creds = './credentials.json'

module.exports = {
    route: 'users/profiles',
    method: 'POST',
    run: async (req, res) => {
        try {
            const { email, name, bio, pfp } = req.query
            const auth = await google.auth.getClient({
                keyFile: creds,
                scopes: ['https://www.googleapis.com/auth/spreadsheets']
            });

            const resource = {
                values: [[email, name, bio, pfp]],
            };
            const spreadsheetId = process.env.DATABASE_ID
            const range = 'Profiles!A:Z'

           const response = await sheets.spreadsheets.values.append({
                auth,
                spreadsheetId,
                range,
                valueInputOption: 'USER_ENTERED',
                resource,
            })
                .then(console.log(`Done`))

                console.log(JSON.stringify(response))

            res.status(200).json({
                status: 200,
                message: "OK",
            })
        } catch (e) {
            console.log(e)
            res.status(500).json({
                status: 200,
                message: "Internal Server Error"
            })
        }
    }
}
aes219 commented 7 months ago

Well, just turns out that the value of "pfp" was exceeding 50000 characters and the Sheets API couldn't handle that much data.