Exemple #1
0
def load_user_account_credentials(try_credentials,
                                  project_id=None,
                                  credentials_path=None):
    """
    Loads user account credentials from a local file.

    .. versionadded 0.2.0

    Parameters
    ----------
    None

    Returns
    -------
    - GoogleCredentials,
        If the credentials can loaded. The retrieved credentials should
        also have access to the project (project_id) on BigQuery.
    - OR None,
        If credentials can not be loaded from a file. Or, the retrieved
        credentials do not have access to the project (project_id)
        on BigQuery.
    """
    import google.auth.transport.requests
    from google.oauth2.credentials import Credentials

    try:
        with open(credentials_path) as credentials_file:
            credentials_json = json.load(credentials_file)
    except (IOError, ValueError):
        return None

    credentials = Credentials(
        token=credentials_json.get("access_token"),
        refresh_token=credentials_json.get("refresh_token"),
        id_token=credentials_json.get("id_token"),
        token_uri=credentials_json.get("token_uri"),
        client_id=credentials_json.get("client_id"),
        client_secret=credentials_json.get("client_secret"),
        scopes=credentials_json.get("scopes"),
    )

    # Refresh the token before trying to use it.
    request = google.auth.transport.requests.Request()
    credentials.refresh(request)

    return try_credentials(project_id, credentials)
Exemple #2
0
 def refresh_cred(cred: Credentials):
     if cred.valid is False:
         cred.refresh(Request())
         print(f"Refreshed credentials for {cred.signer_email}")