gpt4 book ai didi

How to instantiate a Google API service using google-auth?(如何使用google-auth实例化Google API服务?)

转载 作者:bug小助手 更新时间:2023-10-24 23:20:56 27 4
gpt4 key购买 nike



I'm trying to populate a Google sheet from a Python program using its API, using the quickstart guide (https://developers.google.com/sheets/api/quickstart/python) as a starting point:

我正试图使用其应用编程接口,以快速入门指南(https://developers.google.com/sheets/api/quickstart/python)为起点,从一个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()))


However, I was only able to get this to work using the flow_from_clientsecrets() method, which (by default) opens an authentication page in the browser. This does not seem suitable for something I'd like to run periodically on a production server.

然而,我只能使用flow_from_clientsecrets()方法使其工作,该方法(默认情况下)在浏览器中打开一个身份验证页面。这似乎不适合我希望在生产服务器上定期运行的内容。



In any case, according to https://pypi.org/project/oauth2client/, oauth2client is deprecated and developers are recommended to use google-auth instead.

在任何情况下,根据https://pypi.org/project/oauth2client/,的说法,oauth2客户端都是不受欢迎的,建议开发人员使用google-auth。



Therefore, I tried to adapt this example as follows (following https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files):

因此,我尝试将此示例改编如下(遵循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')


Where 'google_client_secret.json' is the JSON file downloaded from the API console, which looks like this (scrambled and pretty-printed):

其中‘google_client_sec.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"
]
}
}


When I run the script, however, I get the following error:

然而,当我运行该脚本时,我得到以下错误:



(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 dropping into the debugger, I noticed that the problem is basically that the dictionary passed into the from_dict() method is the entire dictionary in the google_client_secret.json file, which has only one key, "installed". What the from_dict() method seems to be 'looking for' is the sub-dictionary, as this contains a token_uri key, although even this doesn't contain the required client_email key.

在进入调试器之后,我注意到问题基本上在于传入from_dict()方法的字典是google_client_sec.json文件中的整个字典,该文件只有一个键“Installed”。From_dict()方法似乎要‘寻找’的是子词典,因为它包含一个TOKEN_URI键,尽管它也不包含所需的CLIENT_EMAIL键。



What I'm suspecting is that I've created the wrong type of OAuth2 client for my use case, because the JSON containing the client secrets isn't in the expected format. Any ideas how I could fix this?

我怀疑的是,我为我的用例创建了错误类型的OAuth2客户端,因为包含客户端机密的JSON不是预期的格式。你知道我怎么才能解决这个问题吗?


更多回答
优秀答案推荐

From https://developers.google.com/api-client-library/python/guide/aaa_oauth, there are three types of client IDs:

在https://developers.google.com/api-client-library/python/guide/aaa_oauth,中,有三种类型的客户端ID:




  1. Web application client IDs

  2. Installed application client IDs

  3. Service Account client IDs



My use case is a Service Account, and upon creating one and choosing the Furnish a new private key option, I found that I obtained a JSON file which does match the expected format. (The one I had was for an installed application).

我的用例是一个服务帐户,在创建一个帐户并选择Furish a new Private key选项后,我发现我获得了一个与预期格式匹配的JSON文件。(我的那个是用于已安装的应用程序的)。



You need to fetch the json file, but not from the credentials page.

您需要获取json文件,但不需要从凭据页面获取。


Go to:

请访问:



  1. https://console.cloud.google.com/iam-admin/serviceaccounts (select a project first)

  2. Pick or create a service account from the list

  3. Click the 3 dots and choose "manage keys"

  4. "Add key" > "Create new key", and you'll be able to download the correct json here.


更多回答

@tehhowch, I assume you mean I should carry out point 2: accept the answer? StackOverflow only allows you to accept your own answer after a certain period elapses; in my case, I can only accept it tomorrow. I'll do it then.

@tehhowch,我想你的意思是我应该执行第二点:接受答案?StackOverflow只允许您在一段时间后接受您自己的答案;对我来说,我只能在明天接受它。那我就去做吧。

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