1

我想使用 gspread 并且由于客户端身份验证已过时,我正在尝试使用 Oauth2。我是 gspread 和 Oauth2 的新手。

从这个基本的 Oauth2 示例gspread 文档拼凑起来,我拥有最基本的登录功能。

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”对象不支持索引

从较大的

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 行, 在 gc = gspread.authorize(flow) 文件 "C:\Python34\lib \site-packages\gspread\client.py",第 335 行,在授权 client.login() 文件中 "C:\Python34\lib\site-packages\gspread\client.py",第 105 行,在登录数据 = { 'Email': self.auth[0], TypeError: 'OAuth2WebServerFlow' 对象不支持索引

由于两者都是官方脚本 - 一个来自谷歌,另一个来自 Burnash,我不确定要更改什么。我知道这个问题很基础,但是如何使用 Python 3.4 登录?

4

2 回答 2

1

您可以通过 2 种方式使用 OAUTH 2.0。

  1. 服务帐号

代表您的应用程序而不是最终用户调用 Google API

关注这里了解更多详情:

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
  1. Web应用程序

由网络浏览器通过网络访问

关注此博客了解更多详情

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
于 2015-09-08T02:05:11.143 回答
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
于 2015-09-08T01:56:10.770 回答