【问题标题】:Gspread & Oauth2 on Python 3.4 - Oauth does not support indexingPython 3.4 上的 Gspread 和 Oauth2 - Oauth 不支持索引
【发布时间】:2015-09-05 14:27:44
【问题描述】:

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

拼凑from this basic Oauth2 examplethe 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' 对象不支持索引

从大的

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

""",警告) 回溯(最近一次通话最后): 文件“C:\Users\family\Desktop\mygspread.py”,第 13 行,在 gc = gspread.authorize(流) 文件“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' 对象不支持索引

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

【问题讨论】:

  • 基本上谷歌已经正式宣布“从 2015 年 4 月 20 日起,访问任何谷歌 API 的唯一方法是通过 OAUTH 2.0” 以前,我们可以通过提供我们的电子邮件和密码来访问。目前已贬值。所以,选择 Burnash 的那个。

标签: python-3.x oauth-2.0 gspread


【解决方案1】:

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

  1. 服务帐号

代表您的应用程序而不是结束调用 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
  1. 网络应用程序

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

关注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

【讨论】:

    【解决方案2】:

    我想通了。如果其他人有兴趣,这就是我需要做的

    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-22
      • 2016-12-20
      • 2017-08-17
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多