本文介绍了Gspread &Python 3.4 上的 Oauth2 - Oauth 不支持索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

从这个基本的 Oauth2 示例 和 gspread 文档 我有最基本的登录功能.

导入 gspread从 oauth2client.client 导入 OAuth2WebServerFlowCLIENT_ID = '我的身份证'CLIENT_SECRET = '我的密钥'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

 使用电子邮件和密码的授权将于 2015 年 4 月 20 日停止工作.请改用 oAuth2 授权:http://gspread.readthedocs.org/en/latest/oauth2.html

""", 警告)回溯(最近一次调用最后一次):文件C:\Users\family\Desktop\mygspread.py",第 13 行,在gc = gspread.authorize(flow)文件C:\Python34\lib\site-packages\gspread\client.py",第 335 行,授权客户端登录()文件C:\Python34\lib\site-packages\gspread\client.py",第 105 行,登录数据 = {'Email': self.auth[0],类型错误:'OAuth2WebServerFlow' 对象不支持索引

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

解决方案

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

  1. 服务帐号

代表您的应用程序而不是结束调用 Google API用户

关注此处了解更多详情:

导入json导入 gspread从 oauth2client.client 导入 SignedJwtAssertionCredentialsjson_key = json.load(open('gspread-april-2cd … ba4.json'))范围 = ['https://spreadsheets.google.com/feeds']凭证 = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)gc = gspread.authorize(凭据)wks = gc.open("Lebowski 的钱在哪里?").sheet1
  1. 网络应用

通过网络浏览器访问

关注此博客欲知更多详情

导入请求,gspread从 oauth2client.client 导入 SignedJwtAssertionCredentialsdef authentication_google_docs():f = file(os.path.join('your-key-file.p12'), 'rb')SIGNED_KEY = f.read()f.close()范围 = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']凭证 = SignedJwtAssertionCredentials('[email protected]', SIGNED_KEY, scope)数据 = {'refresh_token' : '','client_id' : '','client_secret' : '','grant_type' : 'refresh_token',}r = requests.post('https://accounts.google.com/o/oauth2/token', 数据 = 数据)credentials.access_token = ast.literal_eval(r.text)['access_token']gc = gspread.authorize(凭据)返回GC

I want to use gspread and since client authentication is outdated, I'm trying with Oauth2. I'm new to both gspread & Oauth2.

Piecing together from this basic Oauth2 example and the gspread documentation I have the most basic login function.

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)

The problem is that I get this error.

from the larger

Since both are official scripts - one from google and the other from burnash, I'm not sure what to change. I know the question is basic, but how do I log in with Python 3.4?

解决方案

You can use OAUTH 2.0 using 2 ways.

  1. Service account

Follow here for more details:

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 application

Follow this blog for more details

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('[email protected]', 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

这篇关于Gspread &amp;Python 3.4 上的 Oauth2 - Oauth 不支持索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 02:33