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

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

ValueError:客户端密码必须用于 Web 或已安装的应用程序。

我创建了一个具有项目所有者权限的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.

解答

问题是我使用的是在服务帐户密钥管理服务帐户下生成的 json,而不是在 OAuth 2.0 客户端 ID 下生成的 json。

按照此讲师创建 oauth 客户端 ID。https://developers.google.com/workspace/guides/create-credentials#oauth-client-id

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐