使用oauth2client进行身份验证时出错

时间:2016-07-25 23:40:45

标签: python python-requests gspread oauth2client google-sheets-api

我有一个Python程序试图使用oauth2client进行Google身份验证,以便我可以使用gspread。这是我的代码:

import sys, os, requests, gspread, ast
from oauth2client.client import SignedJwtAssertionCredentials

f = file(os.path.join('service_account_key.json'), 'rb')
SIGNED_KEY = f.read()
f.close()
scope = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']
credentials = SignedJwtAssertionCredentials('<my email address>', SIGNED_KEY, scope)

data = {
'refresh_token' : '<my refresh toked',
'client_id' : '<my client id>',
'client_secret' : '<my client secret>',
'grant_type' : 'refresh_token',
}

r = requests.post('https://accounts.google.com/o/oauth2/token', data = data)
credentials.access_token = ast.literal_eval(r.text)['access_token']
gc = gspread.authorize(credentials)

此代码主要来自this site,其中还包含我用于设置身份验证的步骤。代码适用于我的笔记本电脑(Windows),但在我的Raspberry Pi(Linux)上,它会引发此错误:

Traceback (most recent call last):
  File "save.py", line 19, in <module>
    r = requests.post('https://accounts.google.com/o/oauth2/token', data = data)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 111, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 467, in send
    raise ConnectionError(e, request = request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='accounts.google.com', port=443): Max retries exceeded with url: /o/oauth2/token (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0xf22290>: Failed to establish a new connection: [Errno -2] Name or service not known',))

有谁知道发生了什么事?

0 个答案:

没有答案