當前位置: 首頁>>代碼示例>>Python>>正文


Python exceptions.DefaultCredentialsError方法代碼示例

本文整理匯總了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 
開發者ID:openvinotoolkit,項目名稱:model_server,代碼行數:19,代碼來源:gs_model.py

示例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 
開發者ID:openvinotoolkit,項目名稱:model_server,代碼行數:25,代碼來源:predict.py

示例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 
開發者ID:pydata,項目名稱:pandas-gbq,代碼行數:20,代碼來源:test_auth.py

示例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 
開發者ID:GoogleCloudPlatform,項目名稱:professional-services,代碼行數:21,代碼來源:gcpohv_cli.py

示例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) 
開發者ID:Kaggle,項目名稱:docker-python,代碼行數:12,代碼來源:test_bigquery.py

示例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 
開發者ID:openvinotoolkit,項目名稱:model_server,代碼行數:17,代碼來源:gs_model.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:7,代碼來源:test__default.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:10,代碼來源:test__default.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:10,代碼來源:test__default.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:10,代碼來源:test__default.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:11,代碼來源:test__default.py

示例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() 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:5,代碼來源:test__default.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:8,代碼來源:test_id_token.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:9,代碼來源:test_id_token.py

示例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. 
開發者ID:firebase,項目名稱:firebase-admin-python,代碼行數:9,代碼來源:test_credentials.py


注:本文中的google.auth.exceptions.DefaultCredentialsError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。