1

I have a JSON file from google spreadsheet (for reading the spreadsheet).

I'm trying to copy the JSON as a string to the script.

this is how I do it :

from oauth2client.service_account import ServiceAccountCredentials
import gspread

   json_string ='''
{
  "type": "service_account",
  "project_id": "ID",
  "private_key_id": "KEYID",
  "private_key": "-----BEGIN PRIVATE KEY-----

 ......KEY HERE......

  -----END PRIVATE KEY-----
  ",
  "client_email": "EMAIL",
  "client_id": "ID",
  "auth_uri": "URI",
  "token_uri": "URI",
  "auth_provider_x509_cert_url": "URL",
  "client_x509_cert_url": "CLINET"
}
  '''

info = json.loads(json_string.replace("'",'"').replace('\r\n', '\\r\\n'), strict=False)
client = gspread.authorize(credentials)
sheet = client.open("Responses").sheet1
...


ERROR OUTPUT -


      private_key_pkcs8_pem = json_data['_private_key_pkcs8_pem']
KeyError: '_private_key_pkcs8_pem'

Any suggestions for how to read the string as JSON credentials correctly?

Omri
  • 673
  • 4
  • 9
  • 25
  • 2
    your code does not show what `json_data` is or where it comes from. The `json_string` which you load into `info` has no key `_private_key_pkcs8_pem` indeed. the key is `private_key` – buran Aug 19 '19 at 07:31
  • @buran I don't think I understand you, I have a `private_key` It's long so I didn't put it in my question. And about `_private_key_pkcs8_pem ` I don't have it on my JSON file from google, so what should I do? – Omri Aug 19 '19 at 08:03
  • the error message refers to this line of code `private_key_pkcs8_pem = json_data['_private_key_pkcs8_pem']` - we don't see it in your code. Nor we see what `json_data` is or where it comes from. Also the error message says key `_private_key_pkcs8_pem` is missing. Note that it does not raise `NameError` for `json_data`, so `json_data` IS defined somewhere else in your code. We also don't see what `credentials` is. Finally you should post the full traceback. Read [mcve] – buran Aug 19 '19 at 08:15

3 Answers3

1

Just an idea, not sure if it's the correct response to your question: You cannot you classic line breaks in JSON. Probably, your private key (or another property you did not include in your code example) contains line breaks and thus the JSON string is invalid.

Read also this Stackoverflow post for more information on how to use line breaks in JSON documents.

SparkFountain
  • 2,110
  • 15
  • 35
1

You need to use ServiceAccountCredentials.from_json_keyfile_dict and not ServiceAccountCredentials.from_json.

Jean-Bernard Jansen
  • 7,544
  • 2
  • 20
  • 17
1

This worked for me, initializing credentials from a string, since we use a remote python client and uploading credential files was not easy.

SCOPES = ('https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/cloud-platform')
json_data = """{
  "type": "service_account",
  "project_id": "xxxxxxxx",
  "private_key_id": "xxxxxxxxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----xxxxxxxx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robotxxxxxxxx.iam.gserviceaccount.com"
}"""
json_key = js.loads(json_data,strict=False)
credentials = service_account.Credentials.from_service_account_info(json_key,scopes=SCOPES)

my_client_py = pg.authorize(custom_credentials=credentials)
Ruli
  • 2,592
  • 12
  • 30
  • 40
user32457
  • 21
  • 1