如何在 google form API 中更新权限或添加电子邮件?

编程入门 行业动态 更新时间:2024-10-08 23:01:40

如何在 google form API 中更新权限或添加<a href=https://www.elefans.com/category/jswz/34/1770696.html style=电子邮件?"/>

如何在 google form API 中更新权限或添加电子邮件?

如何在 google form API 中更新权限或添加电子邮件?
想要添加电子邮件以便能够编辑表单。
我想知道如何设置电子邮件访问谷歌表单的权限?

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: ['','','.file','.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();

我试过访问谷歌表单链接,但我无法访问它。

已尝试 forms.permissions.update 但 google form api 没有添加权限的功能

const res = await google.forms({version: 'v1', auth: authClient}).forms.permissions.update({
  fileId: 'FORM_ID',
  permissionId: 'PERMISSION_ID',
  requestBody: {
    role: 'writer',
    type: 'user',
    emailAddress: '[email protected]',
  },
});
回答如下:

const res = await google.forms({version: 'v1', auth: authClient}).forms.permissions.update({,,,
,您似乎尝试使用Forms API的客户端使用Drive API的方法。我认为这就是您当前问题的原因。并且,在这种情况下,需要使用“权限:创建”而不是“权限:更新”。当这反映在你的脚本中时,它变成如下。

来自:

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

致:

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: "[email protected]", //  Please set your email address.
  },
});
console.log(res.data);
  • 运行此修改后的脚本时,创建的 Google 表单将与
    [email protected]
    共享。如果此电子邮件是 Google 帐户,您可以在“与我共享”处看到 Google 表单。

参考:

  • 权限:创建

更多推荐

如何在 google form API 中更新权限或添加电子邮件?

本文发布于:2024-05-13 14:39:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电子邮件   权限   如何在   form   google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!