-
Notifications
You must be signed in to change notification settings - Fork 943
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use auth.service_account in service account authentication guide.
Add advanced usage documentation stub. Move Authlib example to advanced usage docs.
- Loading branch information
Showing
3 changed files
with
94 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
Advanced Usage | ||
============== | ||
|
||
Custom Authentication | ||
--------------------- | ||
|
||
Using Authlib | ||
~~~~~~~~~~~~~ | ||
|
||
Using ``Authlib`` instead of ``google-auth``. Authlib has an ``AssertionSession`` which can automatically refresh tokens.:: | ||
|
||
import json | ||
from gspread import Client | ||
from authlib.client import AssertionSession | ||
|
||
def create_assertion_session(conf_file, scopes, subject=None): | ||
with open(conf_file, 'r') as f: | ||
conf = json.load(f) | ||
|
||
token_url = conf['token_uri'] | ||
issuer = conf['client_email'] | ||
key = conf['private_key'] | ||
key_id = conf.get('private_key_id') | ||
|
||
header = {'alg': 'RS256'} | ||
if key_id: | ||
header['kid'] = key_id | ||
|
||
# Google puts scope in payload | ||
claims = {'scope': ' '.join(scopes)} | ||
return AssertionSession( | ||
grant_type=AssertionSession.JWT_BEARER_GRANT_TYPE, | ||
token_url=token_url, | ||
issuer=issuer, | ||
audience=token_url, | ||
claims=claims, | ||
subject=subject, | ||
key=key, | ||
header=header, | ||
) | ||
|
||
scopes = [ | ||
'https://www.googleapis.com/auth/spreadsheets', | ||
'https://www.googleapis.com/auth/drive', | ||
] | ||
session = create_assertion_session('your-google-conf.json', scopes) | ||
gc = Client(None, session) | ||
|
||
wks = gc.open("Where is the money Lebowski?").sheet1 | ||
|
||
wks.update_acell('B2', "it's down there somewhere, let me take another look.") | ||
|
||
# Fetch a cell range | ||
cell_list = wks.range('A1:B7') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters