本文整理汇总了Python中google.auth方法的典型用法代码示例。如果您正苦于以下问题:Python google.auth方法的具体用法?Python google.auth怎么用?Python google.auth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google
的用法示例。
在下文中一共展示了google.auth方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retry_auth_check
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def retry_auth_check(exception, verbose):
"""Specific check for auth error codes.
Return True if we should retry.
False otherwise.
Args:
exception: An exception to test for transience.
verbose: If true, output retry messages
Returns:
True if we should retry. False otherwise.
"""
if isinstance(exception, googleapiclient.errors.HttpError):
if exception.resp.status in HTTP_AUTH_ERROR_CODES:
_print_retry_error(exception, verbose)
return True
return False
示例2: setup_service
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def setup_service(api_name, api_version, credentials=None):
"""Configures genomics API client.
Args:
api_name: Name of the Google API (for example: "genomics")
api_version: Version of the API (for example: "v2alpha1")
credentials: Credentials to be used for the gcloud API calls.
Returns:
A configured Google Genomics API client with appropriate credentials.
"""
# dsub is not a server application, so it is ok to filter this warning.
warnings.filterwarnings(
'ignore', 'Your application has authenticated using end user credentials')
if not credentials:
credentials, _ = google.auth.default()
return googleapiclient.discovery.build(
api_name, api_version, credentials=credentials)
示例3: test_default_creds_with_scopes
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_default_creds_with_scopes(self):
self.instance.extras = {
'extra__google_cloud_platform__project': default_project,
'extra__google_cloud_platform__scope': (
','.join(
(
'https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/devstorage.read_only',
)
)
),
}
credentials = self.instance._get_credentials()
if not hasattr(credentials, 'scopes') or credentials.scopes is None:
# Some default credentials don't have any scopes associated with
# them, and that's okay.
return
scopes = credentials.scopes
self.assertIn('https://www.googleapis.com/auth/bigquery', scopes)
self.assertIn(
'https://www.googleapis.com/auth/devstorage.read_only', scopes)
示例4: test_provided_scopes
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_provided_scopes(self):
self.instance.extras = {
'extra__google_cloud_platform__project': default_project,
'extra__google_cloud_platform__scope': (
','.join(
(
'https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/devstorage.read_only',
)
)
),
}
self.assertEqual(
self.instance.scopes,
[
'https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/devstorage.read_only',
],
)
示例5: test_requests
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_requests():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
authed_session.configure_mtls_channel()
# If the devices has default client cert source, then a mutual TLS channel
# is supposed to be created.
assert authed_session.is_mtls == mtls.has_default_client_cert_source()
# Sleep 1 second to avoid 503 error.
time.sleep(1)
if authed_session.is_mtls:
response = authed_session.get(MTLS_ENDPOINT.format(project_id))
else:
response = authed_session.get(REGULAR_ENDPOINT.format(project_id))
assert response.ok
示例6: test_urllib3
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_urllib3():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_http = google.auth.transport.urllib3.AuthorizedHttp(credentials)
is_mtls = authed_http.configure_mtls_channel()
# If the devices has default client cert source, then a mutual TLS channel
# is supposed to be created.
assert is_mtls == mtls.has_default_client_cert_source()
# Sleep 1 second to avoid 503 error.
time.sleep(1)
if is_mtls:
response = authed_http.request("GET", MTLS_ENDPOINT.format(project_id))
else:
response = authed_http.request("GET", REGULAR_ENDPOINT.format(project_id))
assert response.status == 200
示例7: test_requests_with_default_client_cert_source
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_requests_with_default_client_cert_source():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
if mtls.has_default_client_cert_source():
authed_session.configure_mtls_channel(
client_cert_callback=mtls.default_client_cert_source()
)
assert authed_session.is_mtls
# Sleep 1 second to avoid 503 error.
time.sleep(1)
response = authed_session.get(MTLS_ENDPOINT.format(project_id))
assert response.ok
示例8: test_urllib3_with_default_client_cert_source
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_urllib3_with_default_client_cert_source():
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ["https://www.googleapis.com/auth/pubsub"]
)
authed_http = google.auth.transport.urllib3.AuthorizedHttp(credentials)
if mtls.has_default_client_cert_source():
assert authed_http.configure_mtls_channel(
client_cert_callback=mtls.default_client_cert_source()
)
# Sleep 1 second to avoid 503 error.
time.sleep(1)
response = authed_http.request("GET", MTLS_ENDPOINT.format(project_id))
assert response.status == 200
示例9: test_grpc_request_with_jwt_credentials
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_grpc_request_with_jwt_credentials():
credentials, project_id = google.auth.default()
audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher"
credentials = google.auth.jwt.Credentials.from_signing_credentials(
credentials, audience=audience
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例10: test_grpc_request_with_on_demand_jwt_credentials
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_grpc_request_with_on_demand_jwt_credentials():
credentials, project_id = google.auth.default()
credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials(
credentials
)
transport = publisher_grpc_transport.PublisherGrpcTransport(
address=publisher_client.PublisherClient.SERVICE_ADDRESS,
credentials=credentials,
)
# Create a pub/sub client.
client = pubsub_v1.PublisherClient(transport=transport)
# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
list(list_topics_iter)
示例11: _get_gcloud_sdk_credentials
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def _get_gcloud_sdk_credentials():
"""Gets the credentials and project ID from the Cloud SDK."""
from google.auth import _cloud_sdk
# Check if application default credentials exist.
credentials_filename = _cloud_sdk.get_application_default_credentials_path()
if not os.path.isfile(credentials_filename):
return None, None
credentials, project_id = load_credentials_from_file(credentials_filename)
if not project_id:
project_id = _cloud_sdk.get_project_id()
return credentials, project_id
示例12: test_client_library_query_bqstorage
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def test_client_library_query_bqstorage():
# [START bigquery_migration_client_library_query_bqstorage]
import google.auth
from google.cloud import bigquery
from google.cloud import bigquery_storage_v1beta1
# Create a BigQuery client and a BigQuery Storage API client with the same
# credentials to avoid authenticating twice.
credentials, project_id = google.auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
client = bigquery.Client(credentials=credentials, project=project_id)
bqstorage_client = bigquery_storage_v1beta1.BigQueryStorageClient(
credentials=credentials
)
sql = "SELECT * FROM `bigquery-public-data.irs_990.irs_990_2012`"
# Use a BigQuery Storage API client to download results more quickly.
df = client.query(sql).to_dataframe(bqstorage_client=bqstorage_client)
# [END bigquery_migration_client_library_query_bqstorage]
assert len(df) > 0
示例13: clients
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def clients():
# [START bigquerystorage_pandas_tutorial_all]
# [START bigquerystorage_pandas_tutorial_create_client]
import google.auth
from google.cloud import bigquery
from google.cloud import bigquery_storage_v1beta1
# Explicitly create a credentials object. This allows you to use the same
# credentials for both the BigQuery and BigQuery Storage clients, avoiding
# unnecessary API calls to fetch duplicate authentication tokens.
credentials, your_project_id = google.auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
# Make clients.
bqclient = bigquery.Client(
credentials=credentials,
project=your_project_id,
)
bqstorageclient = bigquery_storage_v1beta1.BigQueryStorageClient(
credentials=credentials
)
# [END bigquerystorage_pandas_tutorial_create_client]
# [END bigquerystorage_pandas_tutorial_all]
return bqclient, bqstorageclient
示例14: get_dag_prefix
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def get_dag_prefix(project_id, location, composer_environment):
# [START composer_get_environment_dag_prefix]
import google.auth
import google.auth.transport.requests
# Authenticate with Google Cloud.
# See: https://cloud.google.com/docs/authentication/getting-started
credentials, _ = google.auth.default(
scopes=['https://www.googleapis.com/auth/cloud-platform'])
authed_session = google.auth.transport.requests.AuthorizedSession(
credentials)
# project_id = 'YOUR_PROJECT_ID'
# location = 'us-central1'
# composer_environment = 'YOUR_COMPOSER_ENVIRONMENT_NAME'
environment_url = (
'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}'
'/environments/{}').format(project_id, location, composer_environment)
response = authed_session.request('GET', environment_url)
environment_data = response.json()
# Print the bucket name from the response body.
print(environment_data['config']['dagGcsPrefix'])
# [END composer_get_environment_dag_prefix]
示例15: _get_gcloud_sdk_credentials
# 需要导入模块: import google [as 别名]
# 或者: from google import auth [as 别名]
def _get_gcloud_sdk_credentials():
"""Gets the credentials and project ID from the Cloud SDK."""
from google.auth import _cloud_sdk
# Check if application default credentials exist.
credentials_filename = (
_cloud_sdk.get_application_default_credentials_path())
if not os.path.isfile(credentials_filename):
return None, None
credentials, project_id = _load_credentials_from_file(
credentials_filename)
if not project_id:
project_id = _cloud_sdk.get_project_id()
return credentials, project_id