Example #1
0
def get_credentials(config_data):
    """Decides which type of credentials to return based on the given config.

    Args:
        config_data: a dict containing client configuration.

    Returns:
        An initialized credentials instance.
    """
    required_installed_app_keys = config.get_oauth2_installed_app_keys()
    required_service_account_keys = config.get_oauth2_service_account_keys()

    if all(key in config_data for key in required_installed_app_keys):
        # Using the Installed App Flow
        return get_installed_app_credentials(
            config_data.get("client_id"),
            config_data.get("client_secret"),
            config_data.get("refresh_token"),
        )
    elif all(key in config_data for key in required_service_account_keys):
        # Using the Service Account Flow
        return get_service_account_credentials(
            config_data.get("json_key_file_path"),
            config_data.get("impersonated_email"),
        )
    else:
        raise ValueError(
            "Your YAML file is incorrectly configured for OAuth2. "
            "You need to define credentials for either the OAuth2 "
            "installed application flow ({}) or service account "
            "flow ({}).".format(required_installed_app_keys,
                                required_service_account_keys))
Example #2
0
def get_credentials(config_data):
    """Decides which type of credentials to return based on the given config.

    Args:
        config_data: a dict containing client configuration.

    Returns:
        An initialized credentials instance.
    """
    required_installed_app_keys = config.get_oauth2_installed_app_keys()
    required_service_account_keys = config.get_oauth2_service_account_keys()

    if all(key in config_data for key in required_installed_app_keys):
        # Using the Installed App Flow
        return get_installed_app_credentials(
            config_data.get('client_id'),
            config_data.get('client_secret'),
            config_data.get('refresh_token'))
    elif all(key in config_data for key in required_service_account_keys):
        # Using the Service Account Flow
        return get_service_account_credentials(
            config_data.get('path_to_private_key_file'),
            config_data.get('delegated_account'))
    else:
        raise ValueError('Your YAML file is incorrectly configured for OAuth2. '
                         'You need to define credentials for either the OAuth2 '
                         'installed application flow ({}) or service account '
                         'flow ({}).'.format(required_installed_app_keys,
                                             required_service_account_keys))
 def test_get_oauth2_installed_app_keys(self):
     self.assertEqual(config.get_oauth2_installed_app_keys(),
                      config._OAUTH2_INSTALLED_APP_KEYS)