如何在 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: ['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();
我试过访问谷歌表单链接,但我无法访问它。
已尝试 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);
[email protected]
共享。如果此电子邮件是 Google 帐户,您可以在“与我共享”处看到 Google 表单。