https://developers.google.com/forms/api/guides/update-form-quiz#node.js creates a new Google form using @googleapis/forms and then edit it.
Can someone please provide a sample on how to access an existing Google Form and edit a specific field in it using @googleapis/forms? This is what I tried, but didn't worked. Specifically, there seems to be some problem with the requestBody
I am trying to send in forms.batchUpdate
'use strict';
const path = require('path');
const google = require('@googleapis/forms');
const { authenticate } = require('@google-cloud/local-auth');
const formId = 'some-google-form-id';
async function runSample(query) {
const auth = await authenticate({
keyfilePath: path.join(__dirname, 'credentials.json'),
scopes: 'https://www.googleapis.com/auth/forms.body.readonly',
});
const { forms } = google.forms({
version: 'v1',
auth: auth,
});
const res = await forms.get({ formId });
// console.log(res.data);
console.log(res.data.items[0].questionItem.question.choiceQuestion.options);
res.data.items[0].questionItem.question.choiceQuestion.options = [{ value: 'Australia' }, { value: 'India' }]
const res2 = await forms.batchUpdate({
formId,
requestBody: res,
});
console.log(res2.data.items[0].questionItem.question.choiceQuestion.options);
return res2.data;
}
if (module === require.main) {
runSample().catch(console.error);
}
module.exports = runSample;
Form for above code contains only one drop-down field with test values: click to see image
output on running above snippet:
[ { value: 'Canada' }, { value: 'India' } ]
GaxiosError: Insufficient Permission
at Gaxios._request (/Users/username/learn/google-workspace/node-samples/forms/snippets/node_modules/gaxios/build/src/gaxios.js:130:23)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async OAuth2Client.requestAsync (/Users/username/learn/google-workspace/node-samples/forms/snippets/node_modules/google-auth-library/build/src/auth/oauth2client.js:368:18)
at async runSample (/Users/username/learn/google-workspace/node-samples/forms/snippets/get_form.js:39:16) {
response: {
config: {
url: 'https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU:batchUpdate',
method: 'POST',
userAgentDirectives: [Array],
paramsSerializer: [Function (anonymous)],
data: [Object],
headers: [Object],
params: {},
validateStatus: [Function (anonymous)],
retry: true,
body: '{"config":{"url":"https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU","method":"GET","userAgentDirectives":[{"product":"google-api-nodejs-client","version":"6.0.4","comment":"gzip"}],"headers":{"x-goog-api-client":"gdcl/6.0.4 gl-node/16.16.0 auth/8.2.0","Accept-Encoding":"gzip","User-Agent":"google-api-nodejs-client/6.0.4 (gzip)","Authorization":"Bearer ya29.a0AX9GBdV9dSsJUhNn9FKwOC-RFhqMKEUrbl4xtZUqQPtPNajmhieXS1-zh2LHwmzie16v_XiXYCu2bBibHOMZDT_Oez9gSzSfkRc_peQY-9VxYWohbwsy2Q9SUs2byoRGL_ccCs2WhtG5kRrsQy5GE3PjWBhyaCgYKAToSARMSFQHUCsbCLWi-JGw_W6HLKp_2f6GyLA0163","Accept":"application/json"},"params":{},"retry":true,"responseType":"json"},"data":{"formId":"1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU","info":{"title":"test registrations","description":"test form","documentTitle":"test registrations"},"revisionId":"00000013","responderUri":"https://docs.google.com/forms/d/e/1FAIpQLSeJajF-ymnV5E0p2zE6sDYSdh01P3R57HUkVzr9Ad8q1E1O_Q/viewform","items":[{"itemId":"2ed4bbfe","title":"Venue","questionItem":{"question":{"questionId":"2fb97859","required":true,"choiceQuestion":{"type":"DROP_DOWN","options":[{"value":"Australia"},{"value":"India"}]}}}}]},"headers":{"alt-svc":"h3=\\":443\\"; ma=2592000,h3-29=\\":443\\"; ma=2592000,h3-Q050=\\":443\\"; ma=2592000,h3-Q046=\\":443\\"; ma=2592000,h3-Q043=\\":443\\"; ma=2592000,quic=\\":443\\"; ma=2592000; v=\\"46,43\\"","cache-control":"private","connection":"close","content-encoding":"gzip","content-type":"application/json; charset=UTF-8","date":"Thu, 19 Jan 2023 09:48:10 GMT","server":"ESF","transfer-encoding":"chunked","vary":"Origin, X-Origin, Referer","x-content-type-options":"nosniff","x-frame-options":"SAMEORIGIN","x-xss-protection":"0"},"status":200,"statusText":"OK","request":{"responseURL":"https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU"}}',
responseType: 'json',
retryConfig: [Object]
},
data: { error: [Object] },
headers: {
'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
'cache-control': 'private',
connection: 'close',
'content-encoding': 'gzip',
'content-type': 'application/json; charset=UTF-8',
date: 'Thu, 19 Jan 2023 09:48:11 GMT',
server: 'ESF',
'transfer-encoding': 'chunked',
vary: 'Origin, X-Origin, Referer',
'www-authenticate': 'Bearer realm="https://accounts.google.com/", error="insufficient_scope", scope="https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.resource https://www.googleapis.com/auth/forms.body"',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-xss-protection': '0'
},
status: 403,
statusText: 'Forbidden',
request: {
responseURL: 'https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU:batchUpdate'
}
},
config: {
url: 'https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU:batchUpdate',
method: 'POST',
userAgentDirectives: [ [Object] ],
paramsSerializer: [Function (anonymous)],
data: {
config: [Object],
data: [Object],
headers: [Object],
status: 200,
statusText: 'OK',
request: [Object]
},
headers: {
'x-goog-api-client': 'gdcl/6.0.4 gl-node/16.16.0 auth/8.2.0',
'Accept-Encoding': 'gzip',
'User-Agent': 'google-api-nodejs-client/6.0.4 (gzip)',
Authorization: 'Bearer ya29.a0AX9GBdV9dSsJUhNn9FKwOC-RFhqMKEUrbl4xtZUqQPtPNajmhieXS1-zh2LHwmzie16v_XiXYCu2bBibHOMZDT_Oez9gSzSfkRc_peQY-9VxYWohbwsy2Q9SUs2byoRGL_ccCs2WhtG5kRrsQy5GE3PjWBhyaCgYKAToSARMSFQHUCsbCLWi-JGw_W6HLKp_2f6GyLA0163',
'Content-Type': 'application/json',
Accept: 'application/json'
},
params: {},
validateStatus: [Function (anonymous)],
retry: true,
body: '{"config":{"url":"https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU","method":"GET","userAgentDirectives":[{"product":"google-api-nodejs-client","version":"6.0.4","comment":"gzip"}],"headers":{"x-goog-api-client":"gdcl/6.0.4 gl-node/16.16.0 auth/8.2.0","Accept-Encoding":"gzip","User-Agent":"google-api-nodejs-client/6.0.4 (gzip)","Authorization":"Bearer ya29.a0AX9GBdV9dSsJUhNn9FKwOC-RFhqMKEUrbl4xtZUqQPtPNajmhieXS1-zh2LHwmzie16v_XiXYCu2bBibHOMZDT_Oez9gSzSfkRc_peQY-9VxYWohbwsy2Q9SUs2byoRGL_ccCs2WhtG5kRrsQy5GE3PjWBhyaCgYKAToSARMSFQHUCsbCLWi-JGw_W6HLKp_2f6GyLA0163","Accept":"application/json"},"params":{},"retry":true,"responseType":"json"},"data":{"formId":"1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU","info":{"title":"test registrations","description":"test form","documentTitle":"test registrations"},"revisionId":"00000013","responderUri":"https://docs.google.com/forms/d/e/1FAIpQLSeJajF-ymnV5E0p2zE6sDYSdh01P3R57HUkVzr9Ad8q1E1O_Q/viewform","items":[{"itemId":"2ed4bbfe","title":"Venue","questionItem":{"question":{"questionId":"2fb97859","required":true,"choiceQuestion":{"type":"DROP_DOWN","options":[{"value":"Australia"},{"value":"India"}]}}}}]},"headers":{"alt-svc":"h3=\\":443\\"; ma=2592000,h3-29=\\":443\\"; ma=2592000,h3-Q050=\\":443\\"; ma=2592000,h3-Q046=\\":443\\"; ma=2592000,h3-Q043=\\":443\\"; ma=2592000,quic=\\":443\\"; ma=2592000; v=\\"46,43\\"","cache-control":"private","connection":"close","content-encoding":"gzip","content-type":"application/json; charset=UTF-8","date":"Thu, 19 Jan 2023 09:48:10 GMT","server":"ESF","transfer-encoding":"chunked","vary":"Origin, X-Origin, Referer","x-content-type-options":"nosniff","x-frame-options":"SAMEORIGIN","x-xss-protection":"0"},"status":200,"statusText":"OK","request":{"responseURL":"https://forms.googleapis.com/v1/forms/1lYP-zgoWpnMXRbDFYO48aw3eGLm0dmvIXTtxakjMjjU"}}',
responseType: 'json',
retryConfig: {
currentRetryAttempt: 0,
retry: 3,
httpMethodsToRetry: [Array],
noResponseRetries: 2,
statusCodesToRetry: [Array]
}
},
code: 403,
errors: [
{
message: 'Insufficient Permission',
domain: 'global',
reason: 'insufficientPermissions'
}
]
}
What you're looking for is in the error the API sent you:
In
header > vary
you'll seeThat means you should update the scope to
A More Restricted scope would work according to the specific use case. This should work at a initial level without any trouble.
Note: you may also need to enable the forms API in https://console.cloud.google.com/apis/library/forms.googleapis.com