oauth2client.client.ApplicationDefaultCredentialsError: 'type', 'authorized_user' or 'service_account' values should be set

oauth2client.client.ApplicationDefaultCredentialsError: 'type', 'authorized_user' or 'service_account' values should be set

我正在尝试从我的 Python 脚本访问 BigQuery API。目前我只是在尝试让 this sample 变为 运行。

我已经创建了凭据,设置了环境变量和 运行 脚本,只是为了得到这个错误:

oauth2client.client.ApplicationDefaultCredentialsError: An error was encountered while reading json file: /PATH/TO/MY/google_api_credentials.json (pointed to by GOOGLE_APPLICATION_CREDENTIALS environment variable): 'type' field should be defined (and have one of the 'authorized_user' or 'service_account' values)

嗯,它没有在 JSON 文件中设置:

{"installed":{"client_id":"blahblah.apps.googleusercontent.com","project_id":"blahblah","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"blahblah","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}

因为我从 Google 控制台下载了 JSON,它应该可以正常工作?我不知道 'type' 我需要定义什么。

您提供的 JSON 文件用于 OAuth 客户端。当您使用应用程序默认凭据时,您需要提供 JSON 服务帐户密钥。尝试转到 this page,单击 "Create service account",填写表格,然后选择 "Furnish a new private key."