Exemplo n.º 1
0
def make_config(tmpdir_factory, config):
    # convert to str for python 3.5 compat
    f = Path(str(tmpdir_factory.mktemp("conf").join("google_secret.json")))
    f.write_text(decode(json.dumps(config)))
    return f.parent, f.name
Exemplo n.º 2
0
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:])
Exemplo n.º 3
0
def make_creds(oauth_config, set_oauth_config, creds_json):
    creds_dir = oauth_config[0] / "creds"
    conf.ensure_path(creds_dir)

    creds_dir.joinpath("default").write_text(decode(json.dumps(creds_json)))