Google 工作表 API json 文件 - CLIENT_SECRET 和 oauth2client 凭据有什么区别?
Google Sheets API json files - What is the difference between CLIENT_SECRET and oauth2client credentials?
我遵循了 Google Sheet Python API 快速入门指南 (https://developers.google.com/sheets/api/quickstart/python) 并且能够使用他们提供的代码使其工作:
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'my/path/client_secret.json'
APPLICATION_NAME = 'Google Sheets API Python Quickstart'
credential_path = 'my/path/sheets.googleapis.com-python-quickstart.json'
store = Storage(credential_path)
credentials = store.get()
## !!!!! Is this needed?
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
在默认设置中,我下载了两个 JSON 文件:
client_secret.JSON
- 已下载到
project
目录。
sheets.googleapis.com-python-quickstart.JSON
- 已下载到
~/.credentials
目录
sheets.googleapis.com
JSON 文件开头为:
"_module": "oauth2client.client".
问题 1:每个 JSON 文件的用途是什么?
问题 2:是否需要这两个 JSON 文件才能成功使用 Google Sheets API?
- 我想不,因为我能够在没有
client_secret.JSON
文件的情况下使 API 工作。
这个答案怎么样?我想当你知道 OAuth2 检索访问令牌和刷新令牌的过程时,你就能理解这两个文件的含义。使用 OAuth2 检索访问令牌和刷新令牌的流程如下。
流量:
- 从 API 控制台下载
client_secret.JSON
。
client_secret.JSON
包括 client_id
、client_secret
和 redirect_uris
.
- 使用范围和
client_id
从 client_secret.JSON
检索授权代码。
- 使用授权码
client_id
、client_secret
和 redirect_uris
检索访问令牌和刷新令牌。
- 检索到的访问令牌、刷新令牌和其他参数保存到
sheets.googleapis.com-python-quickstart.JSON
的文件中。
注:
- 当您 运行 第一次 快速入门 时,将启动使用您的浏览器的授权过程。那时,Quickstart 的脚本使用
client_id
和范围检索授权代码,然后使用授权代码检索访问令牌和刷新令牌,client_id
、client_secret
和 redirect_uris
.
- 在快速入门的第一个 运行 之后,访问令牌由
sheets.googleapis.com-python-quickstart.JSON
的刷新令牌检索。这样,不需要使用浏览器检索授权码。所以当有sheets.googleapis.com-python-quickstart.JSON
时,不需要client_secret.JSON
。
- 我认为这会引出您问题 2 的答案。
- 但是,如果您想更改
client_secret.JSON
的范围 and/or 凭据,则需要使用浏览器进行授权并检索授权代码。为此,您必须删除 sheets.googleapis.com-python-quickstart.JSON
并再次授权。那时,在 Quickstart 中,再次使用 client_secret.JSON
。
参考资料:
如果这对你没有用,我很抱歉。
我遵循了 Google Sheet Python API 快速入门指南 (https://developers.google.com/sheets/api/quickstart/python) 并且能够使用他们提供的代码使其工作:
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'my/path/client_secret.json'
APPLICATION_NAME = 'Google Sheets API Python Quickstart'
credential_path = 'my/path/sheets.googleapis.com-python-quickstart.json'
store = Storage(credential_path)
credentials = store.get()
## !!!!! Is this needed?
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
在默认设置中,我下载了两个 JSON 文件:
client_secret.JSON
- 已下载到
project
目录。
- 已下载到
sheets.googleapis.com-python-quickstart.JSON
- 已下载到
~/.credentials
目录
- 已下载到
sheets.googleapis.com
JSON 文件开头为:
"_module": "oauth2client.client".
问题 1:每个 JSON 文件的用途是什么?
问题 2:是否需要这两个 JSON 文件才能成功使用 Google Sheets API?
- 我想不,因为我能够在没有
client_secret.JSON
文件的情况下使 API 工作。
这个答案怎么样?我想当你知道 OAuth2 检索访问令牌和刷新令牌的过程时,你就能理解这两个文件的含义。使用 OAuth2 检索访问令牌和刷新令牌的流程如下。
流量:
- 从 API 控制台下载
client_secret.JSON
。client_secret.JSON
包括client_id
、client_secret
和redirect_uris
.
- 使用范围和
client_id
从client_secret.JSON
检索授权代码。 - 使用授权码
client_id
、client_secret
和redirect_uris
检索访问令牌和刷新令牌。- 检索到的访问令牌、刷新令牌和其他参数保存到
sheets.googleapis.com-python-quickstart.JSON
的文件中。
- 检索到的访问令牌、刷新令牌和其他参数保存到
注:
- 当您 运行 第一次 快速入门 时,将启动使用您的浏览器的授权过程。那时,Quickstart 的脚本使用
client_id
和范围检索授权代码,然后使用授权代码检索访问令牌和刷新令牌,client_id
、client_secret
和redirect_uris
. - 在快速入门的第一个 运行 之后,访问令牌由
sheets.googleapis.com-python-quickstart.JSON
的刷新令牌检索。这样,不需要使用浏览器检索授权码。所以当有sheets.googleapis.com-python-quickstart.JSON
时,不需要client_secret.JSON
。- 我认为这会引出您问题 2 的答案。
- 但是,如果您想更改
client_secret.JSON
的范围 and/or 凭据,则需要使用浏览器进行授权并检索授权代码。为此,您必须删除sheets.googleapis.com-python-quickstart.JSON
并再次授权。那时,在 Quickstart 中,再次使用client_secret.JSON
。
参考资料:
如果这对你没有用,我很抱歉。