【问题标题】:Please confirm: SignedJwtAssertionCredentials only works with SpreadsheetsClient, not SpreadsheetsService?请确认:SignedJwtAssertionCredentials 仅适用于 SpreadsheetsClient,不适用于 SpreadsheetsService?
【发布时间】:2015-02-07 12:56:26
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: python google-sheets gdata oauth2client


    【解决方案1】:

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

    找不到任何将旧的 gdata 代码连接到 oauth 的好例子,您上面的 sn-p 是迄今为止发现的最清晰的。斗争仍在继续。

    【讨论】:

    • 在我的原始帖子之后,我发现了另外一件事:如果我没有从脚本目录运行,则脚本不会像发布的那样工作;它没有找到 PEM 文件。我做了这个修改以纠正: script_dir = os.path.dirname(os.path.realpath (sys.argv[0])) private_key = open(script_dir + "\\IntReqCreds.pem").read( )
    猜你喜欢
    • 2014-07-23
    • 2016-12-23
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多