【发布时间】:2022-07-07 05:13:21
【问题描述】:
我想使用 pentaho 自动化从谷歌提取数据的过程,但在执行脚本期间,浏览器打开并要求选择 gmail 帐户进行身份验证,我在我的 python 代码中使用 OAuth 身份验证。问题在于它会造成障碍,并且需要人工干预来选择用于身份验证的帐户。
如何在 python 代码中避免这种情况,或者是否有另一种不用于浏览器身份验证的身份验证方法。因为在 google 文档中只说明了使用 oauth2 的身份验证。
这是我正在运行的代码:
from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools
from google.auth.transport.requests import Request
SCOPES = "https://www.googleapis.com/auth/forms.responses.readonly"
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"
store = file.Storage('token.json')
creds = None
if not creds or creds.invalid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
print("need to open browser for authentication")
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
creds = tools.run_flow(flow, store)
service = discovery.build('forms', 'v1', http=creds.authorize(
Http()), discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
# Prints the responses of your specified form:
form_id = '<form_id>'
result = service.forms().responses().list(formId=form_id).execute()
print(result)
这是在脚本执行期间打开的浏览器图像
【问题讨论】:
标签: python authentication data-extraction google-forms-api