本文整理汇总了Python中google.auth.exceptions.DefaultCredentialsError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.DefaultCredentialsError方法的具体用法?Python exceptions.DefaultCredentialsError怎么用?Python exceptions.DefaultCredentialsError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.auth.exceptions
的用法示例。
在下文中一共展示了exceptions.DefaultCredentialsError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gs_download_file
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def gs_download_file(path):
if path is None:
return None
parsed_path = urlparse(path)
bucket_name = parsed_path.netloc
file_path = parsed_path.path[1:]
try:
gs_client = storage.Client()
bucket = gs_client.get_bucket(bucket_name)
except exceptions.DefaultCredentialsError:
logger.info('Switching to anonymous google storage client')
gs_client = storage.Client.create_anonymous_client()
bucket = gs_client.bucket(bucket_name, user_project=None)
blob = bucket.blob(file_path)
tmp_path = os.path.join('/tmp', file_path.split(os.sep)[-1])
blob.download_to_filename(tmp_path)
return tmp_path
示例2: get_local_file
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def get_local_file(source_path):
parsed_path = urlparse(source_path)
if parsed_path.scheme == "gs":
bucket_name = parsed_path.netloc
file_path = parsed_path.path[1:]
file_name = os.path.split(parsed_path.path)[1]
try:
gs_client = storage.Client()
bucket = gs_client.get_bucket(bucket_name)
except exceptions.DefaultCredentialsError:
# if credentials fails, try to connect as anonymous user
gs_client = storage.Client.create_anonymous_client()
bucket = gs_client.bucket(bucket_name, user_project=None)
blob = bucket.blob(file_path)
blob.download_to_filename(file_name)
elif parsed_path.scheme == "":
# in case of local path just pass the input argument
if os.path.isfile(source_path):
file_name = source_path
else:
print("file " + source_path + "is not accessible")
file_name = ""
return file_name
示例3: _check_if_can_get_correct_default_credentials
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def _check_if_can_get_correct_default_credentials():
# Checks if "Application Default Credentials" can be fetched
# from the environment the tests are running in.
# See https://github.com/pandas-dev/pandas/issues/13577
import google.auth
from google.auth.exceptions import DefaultCredentialsError
import pandas_gbq.auth
import pandas_gbq.gbq
try:
credentials, project = google.auth.default(
scopes=pandas_gbq.auth.SCOPES
)
except (DefaultCredentialsError, IOError):
return False
return _try_credentials(project, credentials) is not None
示例4: main
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def main():
""" Entry point; invoked if __name__ == '__main__'. Mostly just runs Cli.
"""
args = parse_args()
try:
cli = Cli(args.key_file, args.organization, args.folder, args.use_id)
except exceptions.DefaultCredentialsError as e:
m = 'No credentials provided; use -k or see --help'
raise RuntimeError(m) from e
try:
cli.run()
except errors.HttpError as e:
if e.resp['status'] == '403':
m = ('Permission denied with request to Google API. Ensure '
'account has the correct permissions.')
else:
m = 'Error occurred contacting the Google API'
raise RuntimeError(m) from e
示例5: test_no_project_with_connected_account
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_no_project_with_connected_account(self):
env = EnvironmentVarGuard()
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY')
with env:
with self.assertRaises(DefaultCredentialsError):
# TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project
# by the user or throw a custom exception.
client = bigquery.Client()
self._test_integration(client)
示例6: gs_list_content
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def gs_list_content(path):
parsed_path = urlparse(path)
bucket_name = parsed_path.netloc
model_directory = parsed_path.path[1:]
try:
gs_client = storage.Client()
bucket = gs_client.get_bucket(bucket_name)
except exceptions.DefaultCredentialsError:
gs_client = storage.Client.create_anonymous_client()
bucket = gs_client.bucket(bucket_name, user_project=None)
blobs = bucket.list_blobs(prefix=model_directory)
contents_list = []
for blob in blobs:
contents_list.append(blob.name)
return contents_list
示例7: test_load_credentials_from_missing_file
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_load_credentials_from_missing_file():
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default.load_credentials_from_file("")
assert excinfo.match(r"not found")
示例8: test_load_credentials_from_file_invalid_json
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_load_credentials_from_file_invalid_json(tmpdir):
jsonfile = tmpdir.join("invalid.json")
jsonfile.write("{")
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default.load_credentials_from_file(str(jsonfile))
assert excinfo.match(r"not a valid json file")
示例9: test_load_credentials_from_file_invalid_type
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_load_credentials_from_file_invalid_type(tmpdir):
jsonfile = tmpdir.join("invalid.json")
jsonfile.write(json.dumps({"type": "not-a-real-type"}))
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default.load_credentials_from_file(str(jsonfile))
assert excinfo.match(r"does not have a valid type")
示例10: test_load_credentials_from_file_no_type
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_load_credentials_from_file_no_type(tmpdir):
# use the client_secrets.json, which is valid json but not a
# loadable credentials type
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default.load_credentials_from_file(CLIENT_SECRETS_FILE)
assert excinfo.match(r"does not have a valid type")
assert excinfo.match(r"Type is None")
示例11: test_load_credentials_from_file_authorized_user_bad_format
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_load_credentials_from_file_authorized_user_bad_format(tmpdir):
filename = tmpdir.join("authorized_user_bad.json")
filename.write(json.dumps({"type": "authorized_user"}))
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
_default.load_credentials_from_file(str(filename))
assert excinfo.match(r"Failed to load authorized user")
assert excinfo.match(r"missing fields")
示例12: test_default_fail
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
with pytest.raises(exceptions.DefaultCredentialsError):
assert _default.default()
示例13: test_fetch_id_token_no_cred_json_file
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_fetch_id_token_no_cred_json_file(mock_init, monkeypatch):
monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False)
with pytest.raises(exceptions.DefaultCredentialsError):
request = mock.Mock()
id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
示例14: test_fetch_id_token_invalid_cred_file
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_fetch_id_token_invalid_cred_file(mock_init, monkeypatch):
not_json_file = os.path.join(os.path.dirname(__file__), "../data/public_cert.pem")
monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file)
with pytest.raises(exceptions.DefaultCredentialsError):
request = mock.Mock()
id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
示例15: test_nonexisting_path
# 需要导入模块: from google.auth import exceptions [as 别名]
# 或者: from google.auth.exceptions import DefaultCredentialsError [as 别名]
def test_nonexisting_path(self, app_default):
del app_default
# This does not yet throw because the credentials are lazily loaded.
creds = credentials.ApplicationDefault()
with pytest.raises(exceptions.DefaultCredentialsError):
creds.get_credential() # This now throws.