使用Python OAuth客户端的“ValueError:无效的控制字符”

时间:2016-01-08 15:29:10

标签: python-2.7 oauth gspread oauth2client

我正在尝试将Google电子表格中的数据加载到postgres数据库中。 问题是当我尝试验证我的凭据时,我收到以下错误:

File "/usr/local/lib/python2.7/dist-packages/oauth2client/_openssl_crypt.py", line 117, in from_string
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key)
OpenSSL.crypto.Error: [('PEM routines', 'PEM_read_bio', 'no start line')]

我已按照Using OAuth2 for Authorization中的所有步骤启用了API并创建了一个服务帐户,我从中获得了一个包含必要密钥和身份验证元素的.json文件。

我尝试进行身份验证的方式如下:

json_key = json.load(open('gdoc.json'),strict=False)

creds = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

login = gspread.authorize(creds)

也许问题来自json load strict=False中的参数,问题是如果我删除它,得到错误:

File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 5 column 38 (char 174)

我看到其他几个论坛,他们建议使用strict = False参数,因为json密钥文件中没有转义\ n。

这是.json密钥文件的副本:

{
  "type": "service_account",
  "project_id": "geometric-shine-118101",
  "private_key_id": "xxx",
  "private_key": "-----BEGIN PRIVATE KEY-----\nxxx\n-----END PRIVATE KEY-----\n",
  "client_email": "dataload@geometric-shine-118101.iam.gserviceaccount.com",
  "client_id": "117076930343404252458",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dataload%40geometric-shine-118101.iam.gserviceaccount.com"
}

1 个答案:

答案 0 :(得分:-1)

"private_key": "-----BEGIN PRIVATE KEY-----\nxxx\n-----END PRIVATE KEY-----\n",
 "client_email": "dataload@geometric-shine-118101.iam.gserviceaccount.com",

将实际值复制并粘贴到单引号(.e.g。'')中。它对我有用。

'dataload@geometric-shine-118101.iam.gserviceaccount.com'

'-----BEGIN PRIVATE KEY-----\nxxx\n-----END PRIVATE KEY-----\n'