我刚刚开始使用 google API。作为练习,我正在尝试创建一个自定义表单。我正在使用服务帐户密钥进行身份验证。然后,我构建了一个仅包含一个问题的表格。然后,我尝试通过发送 POST 请求来创建表单。见以下代码:
from google.oauth2 import service_account
from googleapiclient.discovery import build
# set up authentication using the service account key
creds = service_account.Credentials.from_service_account_file(
'my_service_account_key.json',
scopes=['https://www.googleapis.com/auth/forms']
)
service = build('forms', 'v1', credentials=creds)
form_config = {
'title': 'My Custom Form',
'description': 'This is my custom form',
'questions': [
{
'title': 'What is your name?',
'type': 'short_answer'
}
]
}
# create the form by sending a POST request to the API endpoint
response = service.forms().create(body=form_config).execute()
但是,我收到以下错误:
HttpError: <HttpError 403 when requesting https://forms.googleapis.com/v1/forms?alt=json returned "Request had insufficient authentication scopes.".
Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com',
'metadata': {'service': 'forms.googleapis.com', 'method': 'google.apps.forms.v1.FormsService.CreateForm'}}]">
我假设我的服务帐户不知何故没有正确的范围权限,但我找不到任何关于如何更改它的明确信息。我试过为服务帐户提供各种不同的角色。
使用此代码使用没有模拟的服务帐户将导致文件归服务帐户所有,而不是服务帐户所属的用户所有。这将使表单难以查找和使用。要在代码中配置模拟,请确保添加额外的一行,如下所示:
SERVICE_ACCOUNT_FILE = 'serviceaccount.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes= SCOPES)
delegated_creds = credentials.with_subject("[email protected]")
如果您没有 Google Workspace 组织,您应该使用带有 OAuth 同意屏幕的常规 OAuth 2.0。
此外,您使用的范围是
https://www.googleapis.com/auth/forms
但 https://developers.google.com/forms/api/reference/rest/v1/forms/create 的文档声明调用 Forms。创建 method 你需要至少使用其中之一:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/forms.body
此外,文档概述了您需要先创建表单,然后才能添加问题和其他详细信息。使用 Forms.create method 成功创建表单后,您将收到包含创建表单详细信息的回复。
要完成域范围委托的设置,请按照本文档中的步骤https://developers.google.com/workspace/guides/create-credentials#optional_set_up_domain-wide_delegation_for_a_service_account。在“OAuth 范围”的第 5 步中,确保您在代码中使用的范围与管理控制台中授权的范围相同。
经过所有必要的调整,最终代码为:
from google.oauth2 import service_account
from googleapiclient.discovery import build
creds = service_account.Credentials.from_service_account_file(
'serviceaccount.json',
scopes=['https://www.googleapis.com/auth/forms.body']
)
delegated_creds = creds.with_subject("[email protected]")
service = build('forms', 'v1', credentials=delegated_creds)
form_config = {
"info": {
"title": "Newly create Form"
}
}
response = service.forms().create(body=form_config).execute()
print(response)
执行此代码返回此响应:
{
'formId': 'TheNewFormId',
'info': {
'title': 'Newly create Form',
'documentTitle': 'Untitled form'
},
'revisionId': '00000002',
'responderUri': 'https://docs.google.com/forms/d/e/TheNewFormId/viewform'
}
注意response中包含fileId,这个可以用来通过代码更新Form的信息,也可以在WebUI中编辑Form。