You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Google Forms API创建表单时遇“请求身份验证范围不足”错误

解决Google Forms API创建表单的403权限范围错误

问题根源

你碰到的ACCESS_TOKEN_SCOPE_INSUFFICIENT错误,主要来自两个核心问题:

  • 配置的OAuth2权限范围不符合创建表单的要求
  • 你的Google Cloud项目可能未启用Google Forms API

分步修复方案

1. 替换正确的权限范围

将代码中的scope从https://www.googleapis.com/auth/forms替换为https://www.googleapis.com/auth/forms.body——这个权限范围专门用于创建表单及修改表单内容。修正后的认证代码如下:

from google.oauth2 import service_account
from googleapiclient.discovery import build

# 修正后的权限范围
creds = service_account.Credentials.from_service_account_file(
    'my_service_account_key.json',
    scopes=['https://www.googleapis.com/auth/forms.body']
)
service = build('forms', 'v1', credentials=creds)

2. 启用Google Forms API

登录Google Cloud Console,按以下步骤操作:

  • 进入你的项目,搜索并打开「Google Forms API」页面
  • 点击「启用」按钮,等待1-2分钟让API生效

3. 修正表单请求格式

你当前的form_config结构不符合API规范:CreateForm接口只能创建空表单,添加问题需要通过batchUpdate接口完成。正确的代码逻辑如下:

# 创建空表单
form_config = {
  'info': {
    'title': 'My Custom Form',
    'description': 'This is my custom form'
  }
}
response = service.forms().create(body=form_config).execute()
form_id = response['formId']

# 构造添加问题的请求
add_question_request = {
    "requests": [
        {
            "createItem": {
                "item": {
                    "title": "What is your name?",
                    "questionItem": {
                        "question": {
                            "shortAnswerQuestion": {}
                        }
                    }
                },
                "location": {
                    "index": 0
                }
            }
        }
    ]
}

# 发送添加问题的请求
service.forms().batchUpdate(formId=form_id, body=add_question_request).execute()

额外提示

  • 服务账号创建的表单默认归服务账号所有,若需要个人邮箱访问,需通过API给对应邮箱添加权限
  • 确保服务账号密钥文件路径正确,文件未损坏或被修改

内容的提问来源于stack exchange,提问作者Maarten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 00:34:57