设置 Apps 脚本项目

设置 Apps 脚本项目以直接通过 REST 调用调用 Google Form API 非常简单。假设您已经配置了一个 Google Cloud 项目,请执行以下操作:

  1. 新建 Apps 脚本项目。
  2. 更改关联的 Google Cloud 项目编号,以匹配您为 Google Form API 启用的项目。
  3. 修改清单文件 (appsscript.json) 以添加必要的 OAuth 范围。
  4. 添加 Apps 脚本代码以提取 OAuth 令牌,并使用该令牌进行 REST 调用。

下面简要介绍了这些步骤。

创建和配置新的 Apps 脚本项目

  1. 使用您在配置 GCP 项目时使用的同一 Google ID,转到 Apps 脚本信息中心,然后点击新建项目
  2. 打开项目后,点击 项目设置。
  3. 选中在编辑器中显示“appsscript.json”清单文件复选框。
  4. Google Cloud Platform (GCP) 项目部分,点击更改项目,然后输入您为 Forms API 配置的 GCP 项目编号。

您的 Apps 脚本项目现已配置为访问 Google Form API。下一步是添加适当的 OAuth 范围。

添加 OAuth 范围

如要在 Apps 脚本中生成范围正确的 OAuth 令牌,您需要在项目的清单文件中设置所需的范围。

  1. 在编辑器中,打开 appsscript.json
  2. 将作用域添加到清单的正文中。

    {
      ...
    "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"
      ],
     ...
     }
    
  3. 点击 保存项目并根据需要更正所有语法错误。您的项目现在应该能够通过 REST 调用来调用 Google Form API。

添加 Apps 脚本代码以调用 API

在编写代码以调用表单之前,您需要标识自己拥有的具有响应的表单,并记下其表单 ID。修改表单时,您可以在网址中找到表单 ID:

https://docs.google.com/forms/d/<FORM_ID>/edit

如需调用该 API,您需要使用 Apps 脚本 UrlFetchApp 调用。

  1. 打开 Code.gs 并添加以下代码:

    form-api/snippets/Retrieve_all_responses.gs
     function callFormsAPI() {
      console.log('Calling the Forms API!');
      var formId = '<YOUR_FORM_ID>';
    
      // Get OAuth Token
     var OAuthToken = ScriptApp.getOAuthToken();
     console.log('OAuth token is: ' + OAuthToken);
     var formsAPIUrl = 'https://forms.googleapis.com/v1/forms/' + formId + '/' + 'responses';
     console.log('formsAPIUrl is: ' + formsAPIUrl);
     var options = {
        'headers': {
          Authorization: 'Bearer ' + OAuthToken,
          Accept: 'application/json'
        },
        'method': 'get'
      };  
    var response = UrlFetchApp.fetch(formsAPIUrl, options);
     console.log('Response from forms.responses was: ' + response);
    }
  2. YOUR_FORM_ID 替换为您之前记下的值。

    示例:var formId = 'tL5ygBC8zpbTnTp76JCZdIg80hA-cnpbTnTjnsewCKJH';

  3. 点击 Save project,并根据需要更正所有语法错误。

测试代码

  1. 点击 运行
  2. 根据需要使用与之前相同的 Google ID 为项目授权。

启动后,您应该会在执行日志中看到类似于以下内容的响应:

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

后续步骤

使用 Apps 脚本成功调用该 API 后,请参阅参考文档,并尝试对 API 进行其他调用。