使用json API密钥的Oauth2Client服务帐户

时间:2018-03-25 18:07:30

标签: python json google-sheets oauth2client

我正在使用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文件中,所有这些都正确完成。

1 个答案:

答案 0 :(得分:0)

From the documentation here:

classmethod from_json(json_data) Deserialize a JSON-serialized instance.

Inverse to to_json().

Parameters: json_data – dict or string, Serialized JSON (as a string or an already parsed dictionary) representing a credential.

Returns: ServiceAccountCredentials from the serialized data.

So actually accepts 1 argument and not two.

Note that in Python there n+1 arguments (hence you see the error message 2 arguments but three given) because the self is implicitly passed to the method and hence when you call a method on an object instance you are passing the instance itself implicitly plus your arguments explicitly.