【发布时间】:2018-06-10 09:02:30
【问题描述】:
我遵循了 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 正常工作。
【问题讨论】:
标签: python json api credentials google-sheets-api