我想使用gspread,因为客户端身份验证已过时,我尝试使用Oauth2。我对gspread& amp; OAuth2用户。
拼凑from this basic Oauth2 example和the gspread documentation我有最基本的登录功能。
import gspread
from oauth2client.client import OAuth2WebServerFlow
CLIENT_ID = 'my id'
CLIENT_SECRET = 'my secret key'
flow = OAuth2WebServerFlow(client_id= CLIENT_ID,
client_secret= CLIENT_SECRET,
scope='https://docs.google.com/spreadsheets/',
redirect_uri='http://localhost:80')
gc = gspread.authorize(flow)
问题是我收到了这个错误。
TypeError:' OAuth2WebServerFlow' object不支持索引
来自较大的
C:\ Python34 \ lib \ site-packages \ gspread \ client.py:73:警告: 不推荐使用ClientLogin: https://developers.google.com/identity/protocols/AuthForInstalledApps?csw=1
Authorization with email and password will stop working on April 20, 2015. Please use oAuth2 authorization instead: http://gspread.readthedocs.org/en/latest/oauth2.html
""",警告) Traceback(最近一次调用最后一次): 文件" C:\ Users \ family \ Desktop \ mygspread.py",第13行,in gc = gspread.authorize(flow) 文件" C:\ Python34 \ lib \ site-packages \ gspread \ client.py",第335行,授权 client.login() 文件" C:\ Python34 \ lib \ site-packages \ gspread \ client.py",第105行,登录 data = {' Email':self.auth [0], TypeError:' OAuth2WebServerFlow' object不支持索引
由于两者都是官方脚本 - 一个来自谷歌,另一个来自burnash,我不知道该改变什么。我知道问题是基本的,但我如何使用Python 3.4登录?
答案 0 :(得分:0)
我已经弄清楚了。如果其他人有兴趣,这就是我需要做的事情
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open('Gspread-762ec21ac2c5.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email']
, bytes(json_key['private_key']
, 'utf-8')
, scope)
gc = gspread.authorize(credentials)
wks = gc.open("mytestfile").sheet1
答案 1 :(得分:0)
您可以使用两种方式使用OAUTH 2.0。
代表您的应用程序而不是结束来调用Google API 用户
关注here了解详情:
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open('gspread-april-2cd … ba4.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
gc = gspread.authorize(credentials)
wks = gc.open("Where is the money Lebowski?").sheet1
网络浏览器通过网络访问
关注this blog了解详情
import requests, gspread
from oauth2client.client import SignedJwtAssertionCredentials
def authenticate_google_docs():
f = file(os.path.join('your-key-file.p12'), 'rb')
SIGNED_KEY = f.read()
f.close()
scope = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']
credentials = SignedJwtAssertionCredentials('username@gmail.com', SIGNED_KEY, scope)
data = {
'refresh_token' : '<refresh-token-copied>',
'client_id' : '<client-id-copied>',
'client_secret' : '<client-secret-copied>',
'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)
return gc