功能
使用 gspread
从 Google SpreadSheet 读取数据, 方便数据的输入
gspread 文档中除了加载默认的key文件外, 还提供了两种credential的方式:
- google-auth
同时也是 gspread/auth.py
内部实现采用的方式:
import gspread
from google.oauth2.service_account import Credentials
# credentials = Credentials.from_service_account_file(
# 'path/to/the/downloaded/file.json',
# scopes=scopes
# )
credentials = Credentials.from_service_account_info(
keyfile_dict,
scopes=gspread.auth.DEFAULT_SCOPES
)
gc = gspread.authorize(credentials)
- oauth2client [deprecated]
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
# or
credentials = ServiceAccountCredentials.from_json_keyfile_dict(keyfile_dict)
gc = gspread.authorize(credentials)