本文整理汇总了Python中google.oauth2.credentials.refresh方法的典型用法代码示例。如果您正苦于以下问题:Python credentials.refresh方法的具体用法?Python credentials.refresh怎么用?Python credentials.refresh使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.oauth2.credentials
的用法示例。
在下文中一共展示了credentials.refresh方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_refresh_failure_malformed_expire_time
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh_failure_malformed_expire_time(self, mock_donor_credentials):
credentials = self.make_credentials(lifetime=None)
token = "token"
expire_time = (_helpers.utcnow() + datetime.timedelta(seconds=500)).isoformat(
"T"
)
response_body = {"accessToken": token, "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
)
with pytest.raises(exceptions.RefreshError) as excinfo:
credentials.refresh(request)
assert excinfo.match(impersonated_credentials._REFRESH_ERROR)
assert not credentials.valid
assert credentials.expired
示例2: create_grpc_channel
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def create_grpc_channel(target, credentials, ssl_credentials_file=None,
grpc_channel_options=[]):
"""Create and return a gRPC channel.
Args:
credentials(google.oauth2.credentials.Credentials): OAuth2 credentials.
ssl_credentials_file(str): Path to SSL credentials.pem file
(for testing).
grpc_channel_options([(option_name, option_val)]): gRPC channel options.
Returns:
grpc.Channel.
"""
ssl_credentials = None
if ssl_credentials_file:
with open(ssl_credentials_file) as f:
ssl_credentials = grpc.ssl_channel_credentials(f.read())
http_request = google.auth.transport.requests.Request()
# TODO(proppy): figure out if/why we need to force a refresh.
# if yes, consider remove access token from serialized credentials.
credentials.refresh(http_request)
return google.auth.transport.grpc.secure_authorized_channel(
credentials, http_request, target,
ssl_credentials=ssl_credentials,
options=grpc_channel_options)
示例3: test_refresh_success
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh_success(self, use_data_bytes, mock_donor_credentials):
credentials = self.make_credentials(lifetime=None)
token = "token"
expire_time = (
_helpers.utcnow().replace(microsecond=0) + datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
response_body = {"accessToken": token, "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body),
status=http_client.OK,
use_data_bytes=use_data_bytes,
)
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
示例4: test_refresh_failure_unauthorzed
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh_failure_unauthorzed(self, mock_donor_credentials):
credentials = self.make_credentials(lifetime=None)
response_body = {
"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED",
}
}
request = self.make_request(
data=json.dumps(response_body), status=http_client.UNAUTHORIZED
)
with pytest.raises(exceptions.RefreshError) as excinfo:
credentials.refresh(request)
assert excinfo.match(impersonated_credentials._REFRESH_ERROR)
assert not credentials.valid
assert credentials.expired
示例5: test_sign_bytes
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_sign_bytes(self, mock_donor_credentials, mock_authorizedsession_sign):
credentials = self.make_credentials(lifetime=None)
token = "token"
expire_time = (
_helpers.utcnow().replace(microsecond=0) + datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
token_response_body = {"accessToken": token, "expireTime": expire_time}
response = mock.create_autospec(transport.Response, instance=False)
response.status = http_client.OK
response.data = _helpers.to_bytes(json.dumps(token_response_body))
request = mock.create_autospec(transport.Request, instance=False)
request.return_value = response
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
signature = credentials.sign_bytes(b"signed bytes")
assert signature == b"signature"
示例6: main
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def main(api_endpoint, credentials,
device_model_id, device_id, lang, display, verbose,
grpc_deadline, *args, **kwargs):
# Setup logging.
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
# Load OAuth 2.0 credentials.
try:
with open(credentials, 'r') as f:
credentials = google.oauth2.credentials.Credentials(token=None,
**json.load(f))
http_request = google.auth.transport.requests.Request()
credentials.refresh(http_request)
except Exception as e:
logging.error('Error loading credentials: %s', e)
logging.error('Run google-oauthlib-tool to initialize '
'new OAuth 2.0 credentials.')
return
# Create an authorized gRPC channel.
grpc_channel = google.auth.transport.grpc.secure_authorized_channel(
credentials, http_request, api_endpoint)
logging.info('Connecting to %s', api_endpoint)
with SampleTextAssistant(lang, device_model_id, device_id, display,
grpc_channel, grpc_deadline) as assistant:
while True:
query = click.prompt('')
click.echo('<you> %s' % query)
response_text, response_html = assistant.assist(text_query=query)
if display and response_html:
system_browser = browser_helpers.system_browser
system_browser.display(response_html)
if response_text:
click.echo('<@assistant> %s' % response_text)
示例7: test_refresh
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh(authorized_user_file, http_request, token_info):
with open(authorized_user_file, "r") as fh:
info = json.load(fh)
credentials = google.oauth2.credentials.Credentials(
None, # No access token, must be refreshed.
refresh_token=info["refresh_token"],
token_uri=GOOGLE_OAUTH2_TOKEN_ENDPOINT,
client_id=info["client_id"],
client_secret=info["client_secret"],
)
credentials.refresh(http_request)
assert credentials.token
info = token_info(credentials.token)
info_scopes = _helpers.string_to_scopes(info["scope"])
# Canonical list of scopes at https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login
# or do `gcloud auth application-defaut login --help`
canonical_scopes = set(
[
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/cloud-platform",
"openid",
]
)
# When running the test locally, we always have an additional "accounts.reauth" scope.
canonical_scopes_with_reauth = canonical_scopes.copy()
canonical_scopes_with_reauth.add("https://www.googleapis.com/auth/accounts.reauth")
assert set(info_scopes) == canonical_scopes or set(info_scopes) == canonical_scopes_with_reauth
示例8: test_refresh_source_credentials
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh_source_credentials(self, time_skew):
credentials = self.make_credentials(lifetime=None)
# Source credentials is refreshed only if it is expired within
# _helpers.CLOCK_SKEW from now. We add a time_skew to the expiry, so
# source credentials is refreshed only if time_skew <= 0.
credentials._source_credentials.expiry = (
_helpers.utcnow()
+ _helpers.CLOCK_SKEW
+ datetime.timedelta(seconds=time_skew)
)
credentials._source_credentials.token = "Token"
with mock.patch(
"google.oauth2.service_account.Credentials.refresh", autospec=True
) as source_cred_refresh:
expire_time = (
_helpers.utcnow().replace(microsecond=0)
+ datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
response_body = {"accessToken": "token", "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
)
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
# Source credentials is refreshed only if it is expired within
# _helpers.CLOCK_SKEW
if time_skew > 0:
source_cred_refresh.assert_not_called()
else:
source_cred_refresh.assert_called_once()
示例9: test_id_token_success
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_id_token_success(
self, mock_donor_credentials, mock_authorizedsession_idtoken
):
credentials = self.make_credentials(lifetime=None)
token = "token"
target_audience = "https://foo.bar"
expire_time = (
_helpers.utcnow().replace(microsecond=0) + datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
response_body = {"accessToken": token, "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
)
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
id_creds = impersonated_credentials.IDTokenCredentials(
credentials, target_audience=target_audience
)
id_creds.refresh(request)
assert id_creds.token == ID_TOKEN_DATA
assert id_creds.expiry == datetime.datetime.fromtimestamp(ID_TOKEN_EXPIRY)
示例10: test_id_token_from_credential
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_id_token_from_credential(
self, mock_donor_credentials, mock_authorizedsession_idtoken
):
credentials = self.make_credentials(lifetime=None)
token = "token"
target_audience = "https://foo.bar"
expire_time = (
_helpers.utcnow().replace(microsecond=0) + datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
response_body = {"accessToken": token, "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
)
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
id_creds = impersonated_credentials.IDTokenCredentials(
credentials, target_audience=target_audience
)
id_creds = id_creds.from_credentials(target_credentials=credentials)
id_creds.refresh(request)
assert id_creds.token == ID_TOKEN_DATA
示例11: test_id_token_with_target_audience
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_id_token_with_target_audience(
self, mock_donor_credentials, mock_authorizedsession_idtoken
):
credentials = self.make_credentials(lifetime=None)
token = "token"
target_audience = "https://foo.bar"
expire_time = (
_helpers.utcnow().replace(microsecond=0) + datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
response_body = {"accessToken": token, "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
)
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
id_creds = impersonated_credentials.IDTokenCredentials(credentials)
id_creds = id_creds.with_target_audience(target_audience=target_audience)
id_creds.refresh(request)
assert id_creds.token == ID_TOKEN_DATA
assert id_creds.expiry == datetime.datetime.fromtimestamp(ID_TOKEN_EXPIRY)
示例12: test_id_token_with_include_email
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_id_token_with_include_email(
self, mock_donor_credentials, mock_authorizedsession_idtoken
):
credentials = self.make_credentials(lifetime=None)
token = "token"
target_audience = "https://foo.bar"
expire_time = (
_helpers.utcnow().replace(microsecond=0) + datetime.timedelta(seconds=500)
).isoformat("T") + "Z"
response_body = {"accessToken": token, "expireTime": expire_time}
request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
)
credentials.refresh(request)
assert credentials.valid
assert not credentials.expired
id_creds = impersonated_credentials.IDTokenCredentials(
credentials, target_audience=target_audience
)
id_creds = id_creds.with_include_email(True)
id_creds.refresh(request)
assert id_creds.token == ID_TOKEN_DATA
示例13: test_refresh_no_refresh_token
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh_no_refresh_token(self):
request = mock.create_autospec(transport.Request)
credentials_ = credentials.Credentials(token=None, refresh_token=None)
with pytest.raises(exceptions.RefreshError, match="necessary fields"):
credentials_.refresh(request)
request.assert_not_called()
示例14: test_refresh
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_refresh(self, get_auth_access_token):
get_auth_access_token.return_value = "access_token"
cred = credentials.UserAccessTokenCredentials()
cred.refresh(None)
assert cred.token == "access_token"
示例15: test_credentials_with_scopes_requested_refresh_success
# 需要导入模块: from google.oauth2 import credentials [as 别名]
# 或者: from google.oauth2.credentials import refresh [as 别名]
def test_credentials_with_scopes_requested_refresh_success(
self, unused_utcnow, refresh_grant
):
scopes = ["email", "profile"]
token = "token"
expiry = _helpers.utcnow() + datetime.timedelta(seconds=500)
grant_response = {"id_token": mock.sentinel.id_token}
refresh_grant.return_value = (
# Access token
token,
# New refresh token
None,
# Expiry,
expiry,
# Extra data
grant_response,
)
request = mock.create_autospec(transport.Request)
creds = credentials.Credentials(
token=None,
refresh_token=self.REFRESH_TOKEN,
token_uri=self.TOKEN_URI,
client_id=self.CLIENT_ID,
client_secret=self.CLIENT_SECRET,
scopes=scopes,
)
# Refresh credentials
creds.refresh(request)
# Check jwt grant call.
refresh_grant.assert_called_with(
request,
self.TOKEN_URI,
self.REFRESH_TOKEN,
self.CLIENT_ID,
self.CLIENT_SECRET,
scopes,
)
# Check that the credentials have the token and expiry
assert creds.token == token
assert creds.expiry == expiry
assert creds.id_token == mock.sentinel.id_token
assert creds.has_scopes(scopes)
# Check that the credentials are valid (have a token and are not
# expired.)
assert creds.valid