经过一周的谷歌搜索和反复试验,我终于得到了我的 Python 脚本,它在 Google 电子表格中添加了一行以使用 OAuth2。为了其他可能遭受同样创伤的人的利益,这是我的工作代码:
script_dir = os.path.dirname(os.path.realpath (sys.argv[0]))
private_key = open(script_dir + "\\myClient.pem").read()
ssClient = gdata.spreadsheets.client.SpreadsheetsClient()
credentials = SignedJwtAssertionCredentials(CLIENT_EMAIL, private_key, SCOPE)
http = Http()
http = credentials.authorize(http)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
ssClient = auth2token.authorize(ssClient)
ssClient.GetSpreadsheets()
两个注意事项:
- 如果我使用,这不起作用
gdata.spreadsheet.service.SpreadsheetsService()
,但适用于gdata.spreadsheets.client.SpreadsheetsClient()
这不适用于从 Google Developer Console 下载的 .p12 文件,我需要将其转换为 .pem 文件:
openssl pkcs12 -nodes -nocerts -in myClient.p12 -out myClient.pem
有人可以确认确实没有办法使用SignedJwtAssertionCredentials
with SpreadsheetsService
,或者如果有,请解释正确的程序?我几乎尝试了所有我能想到的组合。
谢谢!