-
Notifications
You must be signed in to change notification settings - Fork 952
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `gspread.auth` module. `gspread.auth.oauth()` uses `InstalledAppFlow` from `google_auth_oauthlib` to implement OAuth 2.0 Authorization flow for installed applications. * Handle configuration dir path for Windows as well * Update test requirements
- Loading branch information
Showing
4 changed files
with
109 additions
and
2 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
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,102 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
gspread.auth | ||
~~~~~~~~~~~~ | ||
Simple authentication with OAuth. | ||
""" | ||
|
||
import os | ||
from google.oauth2.credentials import Credentials | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
|
||
from .client import Client | ||
|
||
DEFAULT_SCOPES = [ | ||
'https://www.googleapis.com/auth/spreadsheets', | ||
'https://www.googleapis.com/auth/drive' | ||
] | ||
|
||
READONLY_SCOPES = [ | ||
'https://www.googleapis.com/auth/spreadsheets.readonly', | ||
'https://www.googleapis.com/auth/drive.readonly' | ||
] | ||
|
||
|
||
def get_config_dir(config_dir_name='gspread', os_is_windows=os.name == 'nt'): | ||
"""Construct a config dir path. | ||
By default: | ||
* `%APPDATA%\gspread` on Windows | ||
* `~/.config/gspread` everywhere else | ||
""" | ||
if os_is_windows: | ||
return os.path.join( | ||
os.environ["APPDATA"], | ||
config_dir_name | ||
) | ||
else: | ||
return os.path.join( | ||
os.path.expanduser('~'), | ||
'.config', | ||
config_dir_name | ||
) | ||
|
||
|
||
DEFAULT_CONFIG_DIR = get_config_dir() | ||
|
||
DEFAULT_CREDENTIALS_FILENAME = os.path.join( | ||
DEFAULT_CONFIG_DIR, | ||
'credentials.json' | ||
) | ||
DEFAULT_AUTHORIZED_USER_FILENAME = os.path.join( | ||
DEFAULT_CONFIG_DIR, | ||
'authorized_user.json' | ||
) | ||
|
||
|
||
def _create_installed_app_flow(scopes): | ||
return InstalledAppFlow.from_client_secrets_file( | ||
DEFAULT_CREDENTIALS_FILENAME, | ||
scopes | ||
) | ||
|
||
|
||
def local_server_flow(scopes, port=0): | ||
flow = _create_installed_app_flow(scopes) | ||
return flow.run_local_server(port=port) | ||
|
||
|
||
def console_flow(scopes): | ||
flow = _create_installed_app_flow(scopes) | ||
return flow.run_console() | ||
|
||
|
||
def load_credentials(filename=DEFAULT_AUTHORIZED_USER_FILENAME): | ||
if os.path.exists(filename): | ||
return Credentials.from_authorized_user_file(filename) | ||
|
||
return None | ||
|
||
|
||
def store_credentials( | ||
creds, | ||
filename=DEFAULT_AUTHORIZED_USER_FILENAME, | ||
strip='token' | ||
): | ||
with open(filename, 'w') as f: | ||
f.write(creds.to_json(strip)) | ||
|
||
|
||
def oauth(scopes=DEFAULT_SCOPES, flow=local_server_flow): | ||
creds = load_credentials() | ||
|
||
if not creds: | ||
creds = flow(scopes=scopes) | ||
store_credentials(creds) | ||
|
||
client = Client(auth=creds) | ||
return client |
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 |
---|---|---|
|
@@ -43,7 +43,11 @@ def read(filename): | |
author_email='[email protected]', | ||
url='https://github.com/burnash/gspread', | ||
keywords=['spreadsheets', 'google-spreadsheets'], | ||
install_requires=['requests>=2.2.1', 'google-auth'], | ||
install_requires=[ | ||
'requests>=2.2.1', | ||
'google-auth>=1.12.0', | ||
'google-auth-oauthlib>=0.4.1' | ||
], | ||
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', | ||
classifiers=[ | ||
"Programming Language :: Python", | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
nose | ||
requests[security] | ||
google-auth | ||
google-auth-oauthlib>=0.4.1 | ||
betamax==0.8.1 | ||
-e git+https://github.com/burnash/betamax-json-body-serializer.git#egg=betamax_json_body_serializer |