def get_creds(user="******", config=None, scope=default_scope): """Get google OAuth2Credentials for the given user. If the user doesn't have previous creds, they will go through the OAuth flow to get new credentials which will be saved for use later. Alternatively, it will get credentials from a service account Parameters ---------- user : str Unique key indicating user's credentials. This is not necessary when using a Service Account and will be ignored (Default value = "default") config : dict Optional, own config can be passed in as a dict, otherwise if None is given it will call :meth:`get_config <get_config>` (Default value = None) scope : list Optional, scope to use for Google Auth (Default value = default_scope) Returns ------- """ config = config or get_config() if "private_key_id" in config: return ServiceAccountCredentials.from_json_keyfile_dict(config, scope) if not isinstance(user, basestring): raise ConfigException( "Need to provide a user key as a string if not using a service account" ) if "creds_dir" not in config: raise ConfigException( "Config needs to have the property creds_dir set. " "User credentials will be stored in this location") creds_file = path.join(config["creds_dir"], user) if path.exists(creds_file): return Storage(creds_file).locked_get() if all(key in config for key in ("client_id", "client_secret", "redirect_uris")): flow = OAuth2WebServerFlow( client_id=config["client_id"], client_secret=config["client_secret"], redirect_uri=config["redirect_uris"][0], scope=scope, ) storage = Storage(creds_file) args = argparser.parse_args(args=["--noauth_local_webserver"]) return run_flow(flow, storage, args) raise ConfigException("Unknown config file format")
def get_creds(user=None, config=None, scope=default_scope): """Get google OAuth2Credentials for the given user. If the user doesn't have previous creds, they will go through the OAuth flow to get new credentials which will be saved for use later. Alternatively, it will get credentials from a service account :param user: string indicating user's credentials (Default value = None) :param config: optional, own config can be passed in as a dict, otherwise if None is given it will call :meth:`get_config <get_config>` (Default value = None) :param scope: optional, scope to use for Google Auth (Default value = default_scope) """ config = config or get_config() if 'private_key_id' in config: return ServiceAccountCredentials.from_json_keyfile_dict(config, scope) if user is None: raise ConfigException("Need to provide a user key if not using " "a service account") if 'creds_dir' not in config: raise ConfigException( "Config needs to have the property creds_dir set. " "User credentials will be stored in this location") creds_file = path.join(config['creds_dir'], user) if path.exists(creds_file): return Storage(creds_file).locked_get() if all(key in config for key in ('client_id', 'client_secret', 'redirect_uris')): flow = OAuth2WebServerFlow(client_id=config['client_id'], client_secret=config['client_secret'], redirect_uri=config['redirect_uris'][0], scope=scope) storage = Storage(creds_file) args = argparser.parse_args(args=['--noauth_local_webserver']) return run_flow(flow, storage, args) raise ConfigException("Unknown config file format")
def get_creds(user="******", config=None, scope=default_scope, creds_dir=None, save=True): """Get google google.auth.credentials.Credentials for the given user. If the user doesn't have previous creds, they will go through the OAuth flow to get new credentials which will be saved for later use. Credentials will be saved in config['creds_dir'], if this value is not set, then they will be stored in a folder named ``creds`` in the default config dir (either ~/.config/gspread_pandas or $GSPREAD_PANDAS_CONFIG_DIR) Alternatively, it will get credentials from a service account. Parameters ---------- user : str Unique key indicating user's credentials. This is not necessary when using a ServiceAccount and will be ignored (Default value = "default") config : dict Optional, dict with "client_id", "client_secret", and "redirect_uris" keys for OAuth or "type", "client_email", "private_key", "private_key_id", and "client_id" for a Service Account. If None is passed, it will call :meth:`get_config() <get_config>` (Default value = None) creds_dir : str Optional, directory to load and store creds from/in. If None, it will use the ``creds`` subdirectory in the default config location. (Default value = None) scope : list Optional, scope to use for Google Auth (Default value = default_scope) Returns ------- google.auth.credentials.Credentials Google credentials that can be used with gspread """ config = config or get_config() try: if "private_key_id" in config: return SACredentials.from_service_account_info(config, scopes=scope) if not isinstance(user, basestring): raise ConfigException( "Need to provide a user key as a string if not using a service account" ) if creds_dir is None: creds_dir = get_config_dir() / "creds" creds_file = creds_dir / user if Path(creds_file).exists(): # need to convert Path to string for python 2.7 return OAuthCredentials.from_authorized_user_file(str(creds_file)) flow = InstalledAppFlow.from_client_config( config, scope, redirect_uri="urn:ietf:wg:oauth:2.0:oob") creds = flow.run_console() if save: creds_data = { "refresh_token": creds.refresh_token, "token_uri": creds.token_uri, "client_id": creds.client_id, "client_secret": creds.client_secret, "scopes": creds.scopes, } ensure_path(creds_dir) creds_file.write_text(decode(json.dumps(creds_data))) return creds except Exception: exc_info = sys.exc_info() if "exc_info" in locals(): reraise(ConfigException, *exc_info[1:])