1
votes

I want to create a temporary JSON file to store the google calendar credentials, that were stored in a "job" object. I am using the ServiceAccountCredentials to then get the credentials from the file.

Client = {
    "clientID": job.getClientID(),
    "clientSecret": job.getClientSecret()
}

temp = tempfile.NamedTemporaryFile(mode="w+b", suffix=".json")
complex_data = open(temp.name, "w", encoding="UTF-8")
complex_data.write(json.dumps(Client))
    # data = complex_data.write(json.dumps(Client))
    # z = json.loads(data)
credentials = ServiceAccountCredentials.from_json(
    temp.name
)
``` 

I get the following error:

Traceback (most recent call last):
  File "/var/www/library-offers-google-calendar/main.py", line 209, in <module>
    temp.name
  File "/var/www/library-offers-google-calendar/venv/lib/python3.7/site-packages/oauth2client/service_account.py", line 436, in from_json
    json_data = json.loads(_helpers._from_bytes(json_data))
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
1

1 Answers

0
votes

From the docs, the method ServiceAccountCredentials.from_json accepts a json data rather than a json file name. If you want to use a json file name, you need to use the method ServiceAccountCredentials.from_json_keyfile_name

Or else, you can directly use Client dictionary as a parameter to ServiceAccountCredentials.from_json without creating and reading from temporary file.