-
Notifications
You must be signed in to change notification settings - Fork 958
Using OAuth2 for Authorization
OAuth credentials can be generated in several different ways using the oauth2client library provided by Google. If you are editing spreadsheets for yourself then the easiest way to generate credentials is to use Signed Credentials stored in your application (see example below). If you plan to edit spreadsheets on behalf of others then visit the Google OAuth2 documentation for more information.
- Head to Google Developers Console and create a new project (or select the one you have.)
- Under “API & auth”, in the API enable “Drive API”.
- Go to “Credentials” and choose “New Credentials > Service Account Key”.
You will automatically download a JSON file with this data.
This is how this file may look like:
{
"private_key_id": "2cd … ba4",
"private_key": "-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n",
"client_email": "473 … [email protected]",
"client_id": "473 … hd.apps.googleusercontent.com",
"type": "service_account"
}
You’ll need client_email and private_key.
- Install oauth2client:
pip install --upgrade oauth2client
Depending on your system setup you may need to install PyOpenSSL:
pip install PyOpenSSL
- Now you can read this file, and use the data when constructing your credentials:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('gspread-april-2cd … ba4.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open("Where is the money Lebowski?").sheet1
If using oauth2client < 2.0.0
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'].encode(), scope)
gc = gspread.authorize(credentials)
wks = gc.open("Where is the money Lebowski?").sheet1
Note: Python2 users do not need to encode json_key['private_key']
due to str
and bytes
not being differentiated.
- Go to Google Sheets and share your spreadsheet with an email you
have in your
json_key['client_email']
. Otherwise you’ll get aSpreadsheetNotFound
exception when trying to open it.
If you’re getting the “No crypto library available” exception, make sure
you have PyOpenSSL
library installed in your environment.
If you have another method of authenicating you can easily hack a custom credentials object.
class Credentials (object):
def __init__ (self, access_token=None):
self.access_token = access_token
def refresh (self, http):
# get new access_token
# this only gets called if access_token is None