【问题标题】:Oauth2Client service account using a json API key使用 json API 密钥的 Oauth2Client 服务帐户
【发布时间】:2018-03-25 18:07:30
【问题描述】:

我正在使用 google 的驱动器 API 构建一个应用程序,并且我正在使用 JSON API 密钥。每当我运行以下代码时:

# these are api managing credentials
scope = ['https://spreadsheets.google.com/spreadsheets']
creds = ServiceAccountCredentials.from_json('client_secret', scope)
client = gspread.authorize(creds)  # this connects the the google api using credentials

我收到这条奇怪的错误消息:

File "C:/Users/Will Kaiser's PC/Documents/PyCharm/Projects/ph/main.py", line 16, in <module>
  creds = ServiceAccountCredentials.from_json('client_secret.json', scope)
  TypeError: from_json() takes 2 positional arguments but 3 were given

这对我来说是一个非常奇怪的错误,因为我给函数提供了 2 个参数。我的密钥在 JSON 文件中,所有这些都正确完成。

【问题讨论】:

    标签: python json google-sheets oauth2client


    【解决方案1】:

    来自文档here

    classmethod from_json(json_data) 反序列化一个 JSON 序列化的 实例。

    to_json() 的反函数。

    参数:json_data – dict或字符串,序列化JSON(作为字符串 或已解析的字典)表示凭据。

    返回:来自序列化数据的 ServiceAccountCredentials。

    所以实际上接受 1 个参数而不是 2 个。

    请注意,在 Python 中有 n+1 个参数(因此您会看到错误消息 2 个参数,但给出了三个参数),因为 self 被隐式传递给方法,因此当您在对象实例上调用方法时,您正在传递实例本身隐含地加上您的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 2016-06-26
      • 2017-09-29
      • 1970-01-01
      • 2019-02-03
      • 2018-06-30
      相关资源
      最近更新 更多