2015-09-05 54 views
1

我想使用gspread,並且由於客戶端身份驗證已過時,因此我嘗試使用Oauth2。我是新手gspread & Oauth2。Python 3.4上的Gspread&Oauth2 - Oauth不支持索引

拼在一起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' object does not support indexing

從大

C:\Python34\lib\site-packages\gspread\client.py:73: Warning: ClientLogin is deprecated: 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 

""", Warning) Traceback (most recent call last): File "C:\Users\family\Desktop\mygspread.py", line 13, in gc = gspread.authorize(flow) File "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' object does not support indexing

既然都是官方文字 - 一個由谷歌和burnash其他的,我不知道什麼改變。我知道這個問題是基本的,但我如何使用Python 3.4登錄?

+0

基本上谷歌已經發表正式聲明說:「從2015年4月20日,訪問任何谷歌API的唯一途徑是通過OAuth 2.0用戶」 以前,我們可以通過訪問給我們的電子郵件和密碼。目前它已折舊。所以,與Burnash一起去吧。 – Jordon

回答

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 
0

您可以使用2種方式使用OAUTH 2.0。

  1. 服務帳戶

Calls Google API's on behalf of your application instead of an end user

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 
  • Web應用
  • Accessed by web browsers over the network

    按照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('[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