功能

使用 gspread 从 Google SpreadSheet 读取数据, 方便数据的输入

gspread 文档中除了加载默认的key文件外, 还提供了两种credential的方式:

- google-auth

同时也是 gspread/auth.py 内部实现采用的方式:

  1. import gspread
  2. from google.oauth2.service_account import Credentials
  3. # credentials = Credentials.from_service_account_file(
  4. # 'path/to/the/downloaded/file.json',
  5. # scopes=scopes
  6. # )
  7. credentials = Credentials.from_service_account_info(
  8. keyfile_dict,
  9. scopes=gspread.auth.DEFAULT_SCOPES
  10. )
  11. gc = gspread.authorize(credentials)

- oauth2client [deprecated]

  1. import gspread
  2. from oauth2client.client import GoogleCredentials
  3. gc = gspread.authorize(GoogleCredentials.get_application_default())
  4. # or
  5. credentials = ServiceAccountCredentials.from_json_keyfile_dict(keyfile_dict)
  6. gc = gspread.authorize(credentials)

参考资料

  1. gspread Authentication
  2. google-auth User Guide