Setting up an Apps Script project to call the Google Forms API directly through a REST call is straightforward. Assuming you have already configured a Google Cloud project, do the following:
- Create a new Apps Script project.
- Change the associated Google Cloud project number to match the project you enabled for the Google Forms API.
- Edit the Manifest file (
appsscript.json
) to add the necessary OAuth scopes. - Add Apps Script code to fetch an OAuth token and make a REST call using the token.
Here's a quick walkthrough of these steps.
Create and configure a new Apps Script project
- Using the same Google ID that you configured your GCP project with, go to the Apps Script Dashboard, then click New project.
- Once your project is open, click Project Settings.
- Select the Show "appsscript.json" manifest file in editor checkbox.
- In the Google Cloud Platform (GCP) Project section, click Change project and enter the GCP project number that you configured for the Forms API.
Your Apps Script project is now configured to access the Google Forms API. The next required step is to add the proper OAuth scopes.
Add OAuth scopes
To generate a properly scoped OAuth token in Apps Script, you need to set the required scopes in the project's manifest file.
- In the editor, open
appsscript.json
. Add the scopes to the body of the manifest.
{ ... "oauthScopes": [ "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.readonly", "https://www.googleapis.com/auth/forms.body", "https://www.googleapis.com/auth/forms.body.readonly", "https://www.googleapis.com/auth/forms.responses.readonly" ], ... }
Click
Save project and correct any syntax errors if needed. Your project should now be able to call the Google Forms API via a REST call.
Add Apps Script code to call the API
Before writing the code to call a form, you need to identify a form that you own that has responses and take note of its form ID. The form ID can be found in the URL when editing the form:
https://docs.google.com/forms/d/<FORM_ID>/edit
To call the API, you will use an Apps Script UrlFetchApp
call.
Open Code.gs and add the following code:
Replace
YOUR_FORM_ID
with the value that you noted earlier.Example:
var formId = 'tL5ygBC8zpbTnTp76JCZdIg80hA-cnpbTnTjnsewCKJH';
Click
Save project and correct any syntax errors if needed.
Test the code
- Click Run.
- Authorize the project as needed using the same Google ID as before.
Once it starts, you should see a response in the Execution log similar to this:
Execution started Calling the Forms API! OAuth token is: ya29.a0ARrdaM8IMjtlv… formsAPIUrl is: https://forms.googleapis.com/v1beta/forms/…/responses Response from Forms.responses was: { "responses": [ { "responseId":"...", "createTime": "2021-03-25T01:23:58.146Z", "lastSubmittedTime": "2021-03-25T01:23:58.146607Z", "answers": { "1e9b0ead": { "questionId": "1e9b0ead", "textAnswers": { "answers": [ { "value": "Red" } ] } }, "773ed8f3": { "questionId": "773ed8f3", "textAnswers": { "answers": [ { "value": "Tesla" } ] } } } } ] } Execution completed
Next steps
Once you've successfully called the API with Apps Script, consult the reference documentation and experiment with making other calls to the API.