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

"""、警告) トレースバック (最新の呼び出しが最後): ファイル "C:\Users\family\Desktop\mygspread.py"、13 行目、gc = gspread.authorize(flow) ファイル "C:\Python34\lib \site-packages\gspread\client.py", line 335, in authorize client.login() File "C:\Python34\lib\site-packages\gspread\client.py", line 105, in login data = { 'Email': self.auth[0]、TypeError: 'OAuth2WebServerFlow' オブジェクトはインデックス作成をサポートしていません

どちらも公式のスクリプトなので、1 つは Google から、もう 1 つはburnash からのものなので、何を変更すればよいかわかりません。質問が基本的なものであることはわかっていますが、Python 3.4 でログインするにはどうすればよいですか?

4

2 に答える 2

1

OAUTH 2.0 は 2 つの方法で使用できます。

  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 に答える