In this blog, I would like to help you with generating the google forms using the Google Appscript and running the app script code via Google OAuth 2.0 Playground. For this, first, we need to set write code to generate a form via app script. Kindly follow the following steps.
1. Create the app script project and give it a name. Write the following code to create a form at https://script.google.com
Make sure that the .gs file name and the function name is also the same.
function myFunction() {
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
item.setTitle('What condiments would you like on your hot dog?');
item.setChoices([
item.createChoice('Ketchup'),
item.createChoice('Mustard'),
item.createChoice('Relish')
]);
form.addMultipleChoiceItem()
.setTitle('Do you prefer cats or dogs?')
.setChoiceValues(['Cats','Dogs'])
.showOtherOption(true);
form.addPageBreakItem()
.setTitle('Getting to know you');
form.addDateItem()
.setTitle('When were you born?');
form.addGridItem()
.setTitle('Rate your interests')
.setRows(['Cars', 'Computers', 'Celebrities'])
.setColumns(['Boring', 'So-so', 'Interesting']);
Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
}
2. Run the Script to test if the script is running fine or not.
3. On Run, it may ask you to authorize the app. Authorize the application and then open the Google Forms to test if the sample form is created or not.
4. Next, we need to create credentials so as to access this project via Google OAuth 2.0 Playground and run the script. For this go to Resource -> Could Platform project -> View API console, and do "Create Credentials" and retrieved client ID and client secret.
You can also access the project by clicking on the created project from the pop-up
Or you can directly visit the console https://console.developers.google.com and select your project (Named in the app script).
5. Next, enable the App Script API
Make sure while enabling you have selected the newly created project.
6. Next, you need to create the credentials to access the script from Google OAuth 2.0 Playground.
In the "Authorised JavaScript origins" set "http://localhost"
In the "Authorised redirect URIs" set "https://developers.google.com/oauthplayground" as currently, we will be requiring the authentication response on the Google OAuth Playground. And click on Create.
7. Now visit https://developers.google.com/oauthplayground and click on the Settings Gear. In the drop-down menu, check the "Use your own OAuth credentials" and enter the "OAuth Client ID" and "OAuth Client Secret" received on the step 6
8. Next, in the "Step 1 Select & authorize APIs" section, select the "Apps Script API v1" and further select the "https://www.googleapis.com/auth/forms" option and click Authorize
9. Next, it will ask for the Authorization of the account of whose you wanna access the selected scope. In this, I am using the same account on which the "App Script" for for creation code is created and the same from which the "Client ID" and "Client Secret" is generated.
10. The above step will generate the "Authorization code" and further you can generate the "Refresh token" and the "Access token".
11. Next, we have to consume the services to execute the google app script code. Click on the "List Possible Operations" and then select "Run Scripts"
12. Next, a Syntax will be generated, asking for the script ID which you can find from the google app script project. For this, on the Google App Script project, Click on File > Project Properties and finally, a pop-up will open referring the script ID
13. Enter the script ID in the PlayGround and then set the request Body by clicking on the "Enter Request Body" button. To understand the request body parameters, refer to the document https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run
14. Now click on the "Send Request". On the successful run, a form will be generated the way it generated in step 3
Hope the above information is useful.
References:
https://developers.google.com/apps-script/reference/forms/