gpt4 book ai didi

python - 值错误 : Client secrets must be for a web or installed app

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:51 53 4
gpt4 key购买 nike

我正在 Python Quickstart 下运行 quickstart.py 示例代码我收到以下错误:

ValueError: Client secrets must be for a web or installed app.

我创建了一个具有项目所有者权限的 credentials.json 文件。

错误发生在下面的一段代码中:

if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)

我还注意到没有创建 token.pickle 文件。这是错误输出:

  File "updateSlidev01.py", line 51, in <module>
main()
File "updateSlidev01.py", line 31, in main
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
File "/Library/Python/2.7/site-packages/google_auth_oauthlib/flow.py", line 174, in from_client_secrets_file
return cls.from_client_config(client_config, scopes=scopes, **kwargs)
File "/Library/Python/2.7/site-packages/google_auth_oauthlib/flow.py", line 147, in from_client_config
'Client secrets must be for a web or installed app.')
ValueError: Client secrets must be for a web or installed app.

最佳答案

对于任何来这里的人,因为他们想通过服务帐户而不是这个 Oauth2 客户端 ID 实际连接到 GCP 日历 API,请在原始示例中创建 creds 对象,如下所示:

from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'

credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)

假设服务帐户配置了正确的访问权限,这将在不提示用户确认的情况下访问日历。

关于python - 值错误 : Client secrets must be for a web or installed app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56445257/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com