javascriptnode.jsgoogle-drive-apigoogle-forms-api

How to update permission or add email in google form API?


How to update permission or add email in google form API?
Want to add email to be able to edit the form.
I would like to know how to set up permissions for email access to google form?

const {google} = require('googleapis');
const {JWT} = require('google-auth-library');

// Replace with your own values
const CLIENT_EMAIL = '';
const PRIVATE_KEY =  '';
const FORM_NAME = 'My Form';
const QUESTION_TITLE = 'What is your favorite color?';
const QUESTION_CHOICES = ['Red', 'Green', 'Blue'];

// Set up a JWT client using your credentials
const authClient = new JWT({
  email: CLIENT_EMAIL,
  key: PRIVATE_KEY,
  scopes: ['https://www.googleapis.com/auth/forms','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/forms.body']
});

// Create a new form
async function createForm() {
  try {
    // Authenticate with the Google Forms API
    await authClient.authorize();

    // Create a new form object
    const form = {
        info: {
            title: FORM_NAME , documentTitle: QUESTION_TITLE
        },
    };

    // Call the Google Forms API to create the form
    const response = await google.forms({version: 'v1', auth: authClient}).forms.create({resource: form});
  console.log(`FORM_ID ${response.data.formId}`)
  const res = await google.forms({version: 'v1', auth: authClient}).forms.get({formId: response.data.formId});
  console.log(res.data);
    // Add a multiple choice question to the form
    const question = {
      title: QUESTION_TITLE,
      questionItem: {
        question: {
         required: true,
          choiceQuestion: {
            type:"RADIO",
            options: QUESTION_CHOICES.map((options) => ({ value: options }))
          }
        }
      }
    };

   //  Call the Google Forms API to add the question to the form
    const questionResponse = await google.forms({version: 'v1', auth: authClient}).forms.batchUpdate({
      formId: response.data.formId,
      resource: {
        requests: [{
          createItem: {
            item: question
            ,location: { index: 0 },
          } 
        }, 
    ], 
      }

    });

 
  } catch (err) {
    console.error(err);
  }
}

createForm();

I've tried accessing the google form link, but I'm not able to access it.

enter image description here

have tried forms.permissions.update but google form api doesn't have function to add permissions

const res = await google.forms({version: 'v1', auth: authClient}).forms.permissions.update({
  fileId: 'FORM_ID',
  permissionId: 'PERMISSION_ID',
  requestBody: {
    role: 'writer',
    type: 'user',
    emailAddress: 'user@example.com',
  },
});

Solution

  • At const res = await google.forms({version: 'v1', auth: authClient}).forms.permissions.update({,,,, it seems that you try to use the method of Drive API using the client of Forms API. I think that this is the reason for your current issue. And, in this case, "Permissions: create" is required to be used instead of "Permissions: update". When this is reflected in your script, it becomes as follows.

    From:

    console.log(`FORM_ID ${response.data.formId}`)
    

    To:

    console.log(`FORM_ID ${response.data.formId}`);
    // I added the below script.
    const drive = google.drive({ version: "v3", auth: authClient});
    const res = await drive.permissions.create({
      fileId: response.data.formId,
      requestBody: {
        role: "writer",
        type: "user",
        emailAddress: "user@example.com", //  Please set your email address.
      },
    });
    console.log(res.data);
    

    Reference: