2

经过一周的谷歌搜索和反复试验,我终于得到了我的 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()

两个注意事项:

  1. 如果我使用,这不起作用gdata.spreadsheet.service.SpreadsheetsService(),但适用于gdata.spreadsheets.client.SpreadsheetsClient()
  2. 这不适用于从 Google Developer Console 下载的 .p12 文件,我需要将其转换为 .pem 文件:

    openssl pkcs12 -nodes -nocerts -in myClient.p12 -out myClient.pem
    

有人可以确认确实没有办法使用SignedJwtAssertionCredentialswith SpreadsheetsService,或者如果有,请解释正确的程序?我几乎尝试了所有我能想到的组合。

谢谢!

4

1 回答 1

0

我感觉到你的痛苦,花了一整天的时间试图让我的旧 gdata.spreadsheet.service 代码与 oauth2client.client.SignedJwtAssertionCredentials 一起工作,但仍然没有设法让它工作。它不适用于 SpreadsheetsClient 或 SpreadsheetsService,也不适用于将 .p12 转换为 .pem。看起来它可以工作,(没有身份验证错误消息)但是在执行例如 GetSpreadsheets() 调用时,我得到一个空结果。

找不到将旧 gdata 代码连接到 oauth 的任何好的示例,您上面的代码段是迄今为止最清晰的。斗争还在继续。

于 2014-12-22T15:05:33.083 回答