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.
TypeError: 'OAuth2WebServerFlow' object does not support indexing
from the larger
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
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?
I've figured it out. If anyone else is interested, this is what I needed to do
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
You can use OAUTH 2.0 using 2 ways.
- Service account
Calls Google API's on behalf of your application instead of an end
user
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
- Web application
Accessed by web browsers over the network
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('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