如何使用 google-auth 实例化 Google API 服务?
How to instantiate a Google API service using google-auth?
我正在尝试使用 API 从 Python 程序填充 Google sheet,使用快速入门指南 (https://developers.google.com/sheets/api/quickstart/python) 作为起点:
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
但是,我只能使用 flow_from_clientsecrets()
方法让它工作,该方法(默认情况下)在浏览器中打开一个身份验证页面。这似乎不适合我想在生产服务器上定期 运行 的东西。
无论如何,按照https://pypi.org/project/oauth2client/, oauth2client
is deprecated and developers are recommended to use google-auth
代替。
因此,我尝试按如下方式调整此示例(在 https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files 之后):
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
'google_client_secret.json')
其中 'google_client_secret.json'
是从 API 控制台下载的 JSON 文件,它看起来像这样(乱码和漂亮打印):
{
"installed": {
"client_id": "33e.apps.googleusercontent.com",
"project_id": "nps-survey-1532981793379",
"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": "foobar",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
但是,当我 运行 脚本时,出现以下错误:
(lucy-web-CVxkrCFK) bash-3.2$ python nps.py
Traceback (most recent call last):
File "nps.py", line 25, in <module>
'google_client_secret.json')
File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/google/oauth2/service_account.py", line 209, in from_service_account_file
filename, require=['client_email', 'token_uri'])
File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/google/auth/_service_account_info.py", line 73, in from_filename
return data, from_dict(data, require=require)
File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/google/auth/_service_account_info.py", line 51, in from_dict
'fields {}.'.format(', '.join(missing)))
ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.
从进入调试器开始,我注意到问题基本上是传递给 from_dict()
方法的字典是 [=22= 中的 entire 字典] 文件,其中只有一个键,"installed"
。 from_dict()
方法似乎是 'looking for' 是子词典,因为它包含一个 token_uri
键,尽管它甚至不包含所需的 client_email
键。
我怀疑我为我的用例创建了错误类型的 OAuth2 客户端,因为包含客户端机密的 JSON 不是预期的格式。有什么办法可以解决这个问题吗?
从 https://developers.google.com/api-client-library/python/guide/aaa_oauth 开始,客户端 ID 分为三种类型:
- Web 应用程序客户端 ID
- 已安装的应用程序客户端 ID
- 服务帐户客户端 ID
我的用例是 Service Account,在创建一个并选择 提供新私钥 选项后,我发现我获得了 JSON 与预期格式匹配的文件。 (我有一个是用于已安装的应用程序)。
我正在尝试使用 API 从 Python 程序填充 Google sheet,使用快速入门指南 (https://developers.google.com/sheets/api/quickstart/python) 作为起点:
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
但是,我只能使用 flow_from_clientsecrets()
方法让它工作,该方法(默认情况下)在浏览器中打开一个身份验证页面。这似乎不适合我想在生产服务器上定期 运行 的东西。
无论如何,按照https://pypi.org/project/oauth2client/, oauth2client
is deprecated and developers are recommended to use google-auth
代替。
因此,我尝试按如下方式调整此示例(在 https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files 之后):
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
'google_client_secret.json')
其中 'google_client_secret.json'
是从 API 控制台下载的 JSON 文件,它看起来像这样(乱码和漂亮打印):
{
"installed": {
"client_id": "33e.apps.googleusercontent.com",
"project_id": "nps-survey-1532981793379",
"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": "foobar",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
但是,当我 运行 脚本时,出现以下错误:
(lucy-web-CVxkrCFK) bash-3.2$ python nps.py
Traceback (most recent call last):
File "nps.py", line 25, in <module>
'google_client_secret.json')
File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/google/oauth2/service_account.py", line 209, in from_service_account_file
filename, require=['client_email', 'token_uri'])
File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/google/auth/_service_account_info.py", line 73, in from_filename
return data, from_dict(data, require=require)
File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/google/auth/_service_account_info.py", line 51, in from_dict
'fields {}.'.format(', '.join(missing)))
ValueError: Service account info was not in the expected format, missing fields token_uri, client_email.
从进入调试器开始,我注意到问题基本上是传递给 from_dict()
方法的字典是 [=22= 中的 entire 字典] 文件,其中只有一个键,"installed"
。 from_dict()
方法似乎是 'looking for' 是子词典,因为它包含一个 token_uri
键,尽管它甚至不包含所需的 client_email
键。
我怀疑我为我的用例创建了错误类型的 OAuth2 客户端,因为包含客户端机密的 JSON 不是预期的格式。有什么办法可以解决这个问题吗?
从 https://developers.google.com/api-client-library/python/guide/aaa_oauth 开始,客户端 ID 分为三种类型:
- Web 应用程序客户端 ID
- 已安装的应用程序客户端 ID
- 服务帐户客户端 ID
我的用例是 Service Account,在创建一个并选择 提供新私钥 选项后,我发现我获得了 JSON 与预期格式匹配的文件。 (我有一个是用于已安装的应用程序)。