google-adk 1.4.0__py3-none-any.whl → 1.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,9 +20,20 @@ from __future__ import annotations
20
20
 
21
21
  import json
22
22
  import logging
23
+ import sys
23
24
  from typing import Optional
24
25
 
25
- from a2a import types as a2a_types
26
+ try:
27
+ from a2a import types as a2a_types
28
+ except ImportError as e:
29
+ if sys.version_info < (3, 10):
30
+ raise ImportError(
31
+ 'A2A Tool requires Python 3.10 or above. Please upgrade your Python'
32
+ ' version.'
33
+ ) from e
34
+ else:
35
+ raise e
36
+
26
37
  from google.genai import types as genai_types
27
38
 
28
39
  from ...utils.feature_decorator import working_in_progress
@@ -230,4 +230,3 @@ class AuthCredential(BaseModelWithConfig):
230
230
  http: Optional[HttpAuth] = None
231
231
  service_account: Optional[ServiceAccount] = None
232
232
  oauth2: Optional[OAuth2Auth] = None
233
- google_oauth2_json: Optional[str] = None
@@ -76,11 +76,7 @@ class CredentialManager:
76
76
  self._refresher_registry = CredentialRefresherRegistry()
77
77
 
78
78
  # Register default exchangers and refreshers
79
- from .exchanger.service_account_credential_exchanger import ServiceAccountCredentialExchanger
80
-
81
- self._exchanger_registry.register(
82
- AuthCredentialTypes.SERVICE_ACCOUNT, ServiceAccountCredentialExchanger()
83
- )
79
+ # TODO: support service account credential exchanger
84
80
  from .refresher.oauth2_credential_refresher import OAuth2CredentialRefresher
85
81
 
86
82
  oauth2_refresher = OAuth2CredentialRefresher()
@@ -15,9 +15,7 @@
15
15
  """Credential exchanger module."""
16
16
 
17
17
  from .base_credential_exchanger import BaseCredentialExchanger
18
- from .service_account_credential_exchanger import ServiceAccountCredentialExchanger
19
18
 
20
19
  __all__ = [
21
20
  "BaseCredentialExchanger",
22
- "ServiceAccountCredentialExchanger",
23
21
  ]
@@ -60,27 +60,12 @@ class OAuth2CredentialRefresher(BaseCredentialRefresher):
60
60
  Returns:
61
61
  True if the credential needs to be refreshed, False otherwise.
62
62
  """
63
- # Handle Google OAuth2 credentials (from service account exchange)
64
- if auth_credential.google_oauth2_json:
65
- try:
66
- google_credential = Credentials.from_authorized_user_info(
67
- json.loads(auth_credential.google_oauth2_json)
68
- )
69
- return google_credential.expired and bool(
70
- google_credential.refresh_token
71
- )
72
- except Exception as e:
73
- logger.warning("Failed to parse Google OAuth2 JSON credential: %s", e)
74
- return False
75
63
 
76
64
  # Handle regular OAuth2 credentials
77
- elif auth_credential.oauth2 and auth_scheme:
65
+ if auth_credential.oauth2:
78
66
  if not AUTHLIB_AVIALABLE:
79
67
  return False
80
68
 
81
- if not auth_credential.oauth2:
82
- return False
83
-
84
69
  return OAuth2Token({
85
70
  "expires_at": auth_credential.oauth2.expires_at,
86
71
  "expires_in": auth_credential.oauth2.expires_in,
@@ -105,22 +90,9 @@ class OAuth2CredentialRefresher(BaseCredentialRefresher):
105
90
  The refreshed credential.
106
91
 
107
92
  """
108
- # Handle Google OAuth2 credentials (from service account exchange)
109
- if auth_credential.google_oauth2_json:
110
- try:
111
- google_credential = Credentials.from_authorized_user_info(
112
- json.loads(auth_credential.google_oauth2_json)
113
- )
114
- if google_credential.expired and google_credential.refresh_token:
115
- google_credential.refresh(Request())
116
- auth_credential.google_oauth2_json = google_credential.to_json()
117
- logger.info("Successfully refreshed Google OAuth2 JSON credential")
118
- except Exception as e:
119
- # TODO reconsider whether we should raise error when refresh failed.
120
- logger.error("Failed to refresh Google OAuth2 JSON credential: %s", e)
121
93
 
122
94
  # Handle regular OAuth2 credentials
123
- elif auth_credential.oauth2 and auth_scheme:
95
+ if auth_credential.oauth2 and auth_scheme:
124
96
  if not AUTHLIB_AVIALABLE:
125
97
  return auth_credential
126
98
 
@@ -288,8 +288,7 @@ async def handle_function_calls_live(
288
288
  trace_tool_call(
289
289
  tool=tool,
290
290
  args=function_args,
291
- response_event_id=function_response_event.id,
292
- function_response=function_response,
291
+ function_response_event=function_response_event,
293
292
  )
294
293
  function_response_events.append(function_response_event)
295
294
 
@@ -138,11 +138,6 @@ class MCPTool(BaseAuthenticatedTool):
138
138
  if credential:
139
139
  if credential.oauth2:
140
140
  headers = {"Authorization": f"Bearer {credential.oauth2.access_token}"}
141
- elif credential.google_oauth2_json:
142
- google_credential = Credentials.from_authorized_user_info(
143
- json.loads(credential.google_oauth2_json)
144
- )
145
- headers = {"Authorization": f"Bearer {google_credential.token}"}
146
141
  elif credential.http:
147
142
  # Handle HTTP authentication schemes
148
143
  if (
@@ -178,10 +173,9 @@ class MCPTool(BaseAuthenticatedTool):
178
173
  headers = {"X-API-Key": credential.api_key}
179
174
  elif credential.service_account:
180
175
  # Service accounts should be exchanged for access tokens before reaching this point
181
- # If we reach here, we can try to use google_oauth2_json or log a warning
182
176
  logger.warning(
183
- "Service account credentials should be exchanged for access"
184
- " tokens before MCP session creation"
177
+ "Service account credentials should be exchanged before MCP"
178
+ " session creation"
185
179
  )
186
180
 
187
181
  return headers
@@ -233,7 +233,6 @@ class ToolAuthHandler:
233
233
  AuthCredentialTypes.OPEN_ID_CONNECT,
234
234
  )
235
235
  and not credential.oauth2.access_token
236
- and not credential.google_oauth2_json
237
236
  )
238
237
 
239
238
  async def prepare_auth_credentials(
google/adk/version.py CHANGED
@@ -13,4 +13,4 @@
13
13
  # limitations under the License.
14
14
 
15
15
  # version: major.minor.patch
16
- __version__ = "1.4.0"
16
+ __version__ = "1.4.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: google-adk
3
- Version: 1.4.0
3
+ Version: 1.4.1
4
4
  Summary: Agent Development Kit
5
5
  Author-email: Google LLC <googleapis-packages@google.com>
6
6
  Requires-Python: >=3.9
@@ -2,10 +2,10 @@ google/adk/__init__.py,sha256=sSPQK3r0tW8ahl-k8SXkZvMcbiTbGICCtrw6KkFucyg,726
2
2
  google/adk/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  google/adk/runners.py,sha256=Tqv3HrulZGBO5YZ9bNbtAihDl-SsPv1SnW5nzUBRfZs,18558
4
4
  google/adk/telemetry.py,sha256=0ZHioyg4GD-A4xd2TPB_W1uW2_m5kMQGHosPwCu9cIc,8641
5
- google/adk/version.py,sha256=1bJw7-dWWcu5lzJG0xHN5h9smUy3_7Hc4dZY0XZLkug,626
5
+ google/adk/version.py,sha256=204Nt6sEgRq2nGzXu_YAVnNA2p7LJ05QFNcBial7Gqw,626
6
6
  google/adk/a2a/__init__.py,sha256=Q9FlRO2IfSE9yEaiAYzWkOMBJPCaNYqh4ihcp0t0BQs,574
7
7
  google/adk/a2a/converters/__init__.py,sha256=Q9FlRO2IfSE9yEaiAYzWkOMBJPCaNYqh4ihcp0t0BQs,574
8
- google/adk/a2a/converters/part_converter.py,sha256=2SxRkKRlhOih7mg33uefNVlBhOYOcOMomQhLSuV3_Kc,5056
8
+ google/adk/a2a/converters/part_converter.py,sha256=lZXhEnnFGSbxJDDo_mClGISIiRxk9Rv0bwC0NtYS4Hw,5286
9
9
  google/adk/agents/__init__.py,sha256=WsCiBlvI-ISWrcntboo_sULvVJNwLNxXCe42UGPLKdY,1041
10
10
  google/adk/agents/active_streaming_tool.py,sha256=vFuh_PkdF5EyyneBBJ7Al8ojeTIR3OtsxLjckr9DbXE,1194
11
11
  google/adk/agents/base_agent.py,sha256=fCwAcR12IVVx8qXWGVf773zOmQEKnXDPwmoYYQwUfR4,12168
@@ -25,25 +25,24 @@ google/adk/artifacts/base_artifact_service.py,sha256=H-t5nckLTfr330utj8vxjH45z81
25
25
  google/adk/artifacts/gcs_artifact_service.py,sha256=-YU4NhZiGMnHHCg00aJWgKq4JWkQLh7EH5OuGusM5bE,5608
26
26
  google/adk/artifacts/in_memory_artifact_service.py,sha256=Iw34Ja89JwGgd3sulbxxk5pVMqzEZJCt4F2m15MC37U,4059
27
27
  google/adk/auth/__init__.py,sha256=GoFe0aZGdp0ExNE4rXNn1RuXLaB64j7Z-2C5e2Hsh8c,908
28
- google/adk/auth/auth_credential.py,sha256=w_icK-fWNyhk_tr-mhX97tzqjuxmWIDnurmTQgRvLEo,7055
28
+ google/adk/auth/auth_credential.py,sha256=F1cMHe_gda5N6RXlyB0IYLLos-Jz-WcNFkIm9SjSiGQ,7012
29
29
  google/adk/auth/auth_handler.py,sha256=aUJrY8fzPvSZwi1rtB_N7fHuHt6DE-H8D8HnkDUCyZQ,6828
30
30
  google/adk/auth/auth_preprocessor.py,sha256=RleOG5I7L1EWVRdX_bC1WtKnt0FDKAcXSSh1RexJqtE,4309
31
31
  google/adk/auth/auth_schemes.py,sha256=dxx9bxjOWoae1fSVxbpaVTwa0I4v76_QJJFEX--1ueA,2260
32
32
  google/adk/auth/auth_tool.py,sha256=2N-lR5UrgBhwPaErwRusT1MrNDQGb8fuiMHO5x3cmck,3674
33
- google/adk/auth/credential_manager.py,sha256=OSJxYMJqTotyK4UueLeDDdmUfGuMuPC_nmwHjifPC_s,9671
33
+ google/adk/auth/credential_manager.py,sha256=G5iqpStGHl4wZxkeqNDNljrEt1L_XNSfNVM9mmOqUhg,9503
34
34
  google/adk/auth/oauth2_credential_util.py,sha256=gIb1OUGA4NZRnIeLLk29T_Q9J8ibTxe7-357UhCW3FY,3340
35
35
  google/adk/auth/credential_service/__init__.py,sha256=Q9FlRO2IfSE9yEaiAYzWkOMBJPCaNYqh4ihcp0t0BQs,574
36
36
  google/adk/auth/credential_service/base_credential_service.py,sha256=4SK1UW9NiLNLrc_grpG3xyIPXySGpl0DIv9bTm-onWE,2325
37
37
  google/adk/auth/credential_service/in_memory_credential_service.py,sha256=xXG-3_uq3CU1Eilp8NGBhje0X77ROlAT5BKyJmLLyHg,2161
38
- google/adk/auth/exchanger/__init__.py,sha256=HCZ8KUqeXORwhjXqHCuezCrXsyVYliuo8YrLXvxS9wY,845
38
+ google/adk/auth/exchanger/__init__.py,sha256=RHCK_Zg7hXzBKvz2Vrwvbx_cMXWittidIZToszaL3Nc,720
39
39
  google/adk/auth/exchanger/base_credential_exchanger.py,sha256=Uqzs_NhEDmuH5n0U_ES5fHlMSagyYEc5JKu-5GdOC_A,1644
40
40
  google/adk/auth/exchanger/credential_exchanger_registry.py,sha256=Nsk9BMmhFbZsXQwPckm8elXfbk6iIRSrvegR6DpcONo,1855
41
41
  google/adk/auth/exchanger/oauth2_credential_exchanger.py,sha256=FA0EYC-zfD1gO9CV9xDYYfPxU2EAJdYwVyWjVy3C6Bw,3649
42
- google/adk/auth/exchanger/service_account_credential_exchanger.py,sha256=AYVZcvOuOhNKPgwwgSOcxyZegi6xq4IW8vvNs_IMvXE,3804
43
42
  google/adk/auth/refresher/__init__.py,sha256=DEEkESlvEteCpO4QcDExm6K8S8y7l_oS-A2TK1Oh1xU,720
44
43
  google/adk/auth/refresher/base_credential_refresher.py,sha256=oDWBAuSnt5-00f8LwTrwTptuwkkUXknad8Zbp2mmOA4,2192
45
44
  google/adk/auth/refresher/credential_refresher_registry.py,sha256=ioHclgxCtpSeiUcMq9jwzPn0IzzxrQLpWSpHy90folA,1877
46
- google/adk/auth/refresher/oauth2_credential_refresher.py,sha256=TqpUMOo_Ln1bZSFYaTiHonSlUvb5-3Mn9npnYC2dF6g,5329
45
+ google/adk/auth/refresher/oauth2_credential_refresher.py,sha256=HARpIwwXNxqo4_CNDpRFrN-p7RsnShgu21hgRFVwv4Q,4035
47
46
  google/adk/cli/__init__.py,sha256=ouPYnIY02VmGNfpA6IT8oSQdfeZd1LHVoDSt_x8zQPU,609
48
47
  google/adk/cli/__main__.py,sha256=gN8rRWlkh_3gLI-oYByxrKpCW9BIfDwrr0YuyisxmHo,646
49
48
  google/adk/cli/agent_graph.py,sha256=Kj5_a4UE1QXmqdRv4i4LI4hKHOrLkBS22Q759F3aRug,9879
@@ -116,7 +115,7 @@ google/adk/flows/llm_flows/auto_flow.py,sha256=CnuFelyZhB_ns4U_5_dW0x_KQlzu02My7
116
115
  google/adk/flows/llm_flows/base_llm_flow.py,sha256=uGqJ-b_Xygqgae1dndNh9tgTaj3luMKq3Qj4_jDf278,22660
117
116
  google/adk/flows/llm_flows/basic.py,sha256=263pfk6PIqKO-7BWo--bsBbpZJ8u5R9OK__WhpBgIbM,2848
118
117
  google/adk/flows/llm_flows/contents.py,sha256=bAklBI8YWctd0pGQCRwCVDqDxASiCNV_t8tJChPLbFg,13055
119
- google/adk/flows/llm_flows/functions.py,sha256=r082klTW5MRHs8H3e5DgkeYwBDk8kEw583UsnqINiZk,17556
118
+ google/adk/flows/llm_flows/functions.py,sha256=NRzs9MfqCI8JjKBJCPk3iO_xXn0PzaBfy7NjQrdVsUU,17512
120
119
  google/adk/flows/llm_flows/identity.py,sha256=X4CRg12NvnopmydU9gbFJI4lW1_otN-w_GOAuPvKrXo,1651
121
120
  google/adk/flows/llm_flows/instructions.py,sha256=sO2dQ5hn6ybjXs2fWYWvEFVtACdpiiP0yKf9eNVjhhM,2879
122
121
  google/adk/flows/llm_flows/single_flow.py,sha256=gC677SxxammKx1XkZBzUdgBjDzeymKRcRQQxFGIur8Y,1904
@@ -207,7 +206,7 @@ google/adk/tools/google_api_tool/googleapi_to_openapi_converter.py,sha256=mo1ew3
207
206
  google/adk/tools/mcp_tool/__init__.py,sha256=UIXmz81_7s-kpZNSTOhpWXtQeMxXpjTwMRoDPDbCTkQ,1515
208
207
  google/adk/tools/mcp_tool/conversion_utils.py,sha256=PfPSBAPzAgBsEWk2goKOFHz4fM-9ralW-nMkCbs0b38,5339
209
208
  google/adk/tools/mcp_tool/mcp_session_manager.py,sha256=9QRcprgzfpUHBTePXE2rrUb3LKRQeGo38-Gb6LPbH2w,12946
210
- google/adk/tools/mcp_tool/mcp_tool.py,sha256=rqPTmd4xf5-oQjjLtdqwtAmjaYemZ6RlSdh3Y5iOR2A,6427
209
+ google/adk/tools/mcp_tool/mcp_tool.py,sha256=9tHuqpjBKqcMeI8WvVXfWuWvxov8kzuG85REboblpL8,6081
211
210
  google/adk/tools/mcp_tool/mcp_toolset.py,sha256=dp3IhfdrK07CvZ5LERVtUBzlbbUOhnrmI0ND8Fn2eGk,6323
212
211
  google/adk/tools/openapi_tool/__init__.py,sha256=UMsewNCQjd-r1GBX1OMuUJTzJ0AlQuegIc98g04-0oU,724
213
212
  google/adk/tools/openapi_tool/auth/__init__.py,sha256=NVRXscqN4V0CSCvIp8J_ee8Xyw4m-OGoZn7SmrtOsQk,637
@@ -224,7 +223,7 @@ google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py,sha256=
224
223
  google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py,sha256=zxQtizsdW8FIqhpSzGfhkQLbBXIDZOK_0h8TrXayYZ4,5532
225
224
  google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py,sha256=PhgkKRtSQi-gZa2RBeEzCX0A0Aekk2kLIo_cuf9aAQ0,9078
226
225
  google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py,sha256=TMLDr-ODSEiok0l1ObPmuwcTOvOAjuxoMNBw_QwG5Pg,14650
227
- google/adk/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py,sha256=ALVwmWZzvc9kvqp3lBovZ_N4_LY0F1CrPUItNn-PXFI,10059
226
+ google/adk/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py,sha256=9bqnlvmcr6i8Dab73ntzbynkFFWCYMhk7R-DS-jkOz4,10013
228
227
  google/adk/tools/retrieval/__init__.py,sha256=0euJjx0ReH8JmUI5-JU8kWRswqLxobRCDjx5zvX4rHY,1188
229
228
  google/adk/tools/retrieval/base_retrieval_tool.py,sha256=4aar8Kg-6rQG7Ht1n18D5fvJnuffodFdSjeCp-GzA7w,1174
230
229
  google/adk/tools/retrieval/files_retrieval.py,sha256=UvxXjs3t8O2VO7o4wagHah2ydHT6sl0bLMsKxDVTOHU,1271
@@ -234,8 +233,8 @@ google/adk/utils/__init__.py,sha256=Q9FlRO2IfSE9yEaiAYzWkOMBJPCaNYqh4ihcp0t0BQs,
234
233
  google/adk/utils/feature_decorator.py,sha256=DzGHMTStf4-S9BNiA4EqcCJbrdKijhgeSUSPdzM44H8,5048
235
234
  google/adk/utils/instructions_utils.py,sha256=al9Z-P8qOrbxNju8cqkeH7qRg0oQH7hfmvTG-0oSAQk,3996
236
235
  google/adk/utils/variant_utils.py,sha256=u9IuOn2aXG3ibDYshgLoogBXqH9Gd84ixArQoeLQiE8,1463
237
- google_adk-1.4.0.dist-info/entry_points.txt,sha256=zL9CU-6V2yQ2oc5lrcyj55ROHrpiIePsvQJ4H6SL-zI,43
238
- google_adk-1.4.0.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
239
- google_adk-1.4.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
240
- google_adk-1.4.0.dist-info/METADATA,sha256=ZJf1XYtlb1017k3bc5mNCXcOQuIwkevN2Lvx1avtj20,9981
241
- google_adk-1.4.0.dist-info/RECORD,,
236
+ google_adk-1.4.1.dist-info/entry_points.txt,sha256=zL9CU-6V2yQ2oc5lrcyj55ROHrpiIePsvQJ4H6SL-zI,43
237
+ google_adk-1.4.1.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
238
+ google_adk-1.4.1.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
239
+ google_adk-1.4.1.dist-info/METADATA,sha256=tt1qvxkFHQe0DgLQdGdiBikrs9VHhSAtbJqyT4i7rHQ,9981
240
+ google_adk-1.4.1.dist-info/RECORD,,
@@ -1,104 +0,0 @@
1
- # Copyright 2025 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- """Credential fetcher for Google Service Account."""
16
-
17
- from __future__ import annotations
18
-
19
- from typing import Optional
20
-
21
- import google.auth
22
- from google.auth.transport.requests import Request
23
- from google.oauth2 import service_account
24
- from typing_extensions import override
25
-
26
- from ...utils.feature_decorator import experimental
27
- from ..auth_credential import AuthCredential
28
- from ..auth_credential import AuthCredentialTypes
29
- from ..auth_schemes import AuthScheme
30
- from .base_credential_exchanger import BaseCredentialExchanger
31
-
32
-
33
- @experimental
34
- class ServiceAccountCredentialExchanger(BaseCredentialExchanger):
35
- """Exchanges Google Service Account credentials for an access token.
36
-
37
- Uses the default service credential if `use_default_credential = True`.
38
- Otherwise, uses the service account credential provided in the auth
39
- credential.
40
- """
41
-
42
- @override
43
- async def exchange(
44
- self,
45
- auth_credential: AuthCredential,
46
- auth_scheme: Optional[AuthScheme] = None,
47
- ) -> AuthCredential:
48
- """Exchanges the service account auth credential for an access token.
49
-
50
- If the AuthCredential contains a service account credential, it will be used
51
- to exchange for an access token. Otherwise, if use_default_credential is True,
52
- the default application credential will be used for exchanging an access token.
53
-
54
- Args:
55
- auth_scheme: The authentication scheme.
56
- auth_credential: The credential to exchange.
57
-
58
- Returns:
59
- An AuthCredential in OAUTH2 format, containing the exchanged credential JSON.
60
-
61
- Raises:
62
- ValueError: If service account credentials are missing or invalid.
63
- Exception: If credential exchange or refresh fails.
64
- """
65
- if auth_credential is None:
66
- raise ValueError("Credential cannot be None.")
67
-
68
- if auth_credential.auth_type != AuthCredentialTypes.SERVICE_ACCOUNT:
69
- raise ValueError("Credential is not a service account credential.")
70
-
71
- if auth_credential.service_account is None:
72
- raise ValueError(
73
- "Service account credentials are missing. Please provide them."
74
- )
75
-
76
- if (
77
- auth_credential.service_account.service_account_credential is None
78
- and not auth_credential.service_account.use_default_credential
79
- ):
80
- raise ValueError(
81
- "Service account credentials are invalid. Please set the"
82
- " service_account_credential field or set `use_default_credential ="
83
- " True` to use application default credential in a hosted service"
84
- " like Google Cloud Run."
85
- )
86
-
87
- try:
88
- if auth_credential.service_account.use_default_credential:
89
- credentials, _ = google.auth.default()
90
- else:
91
- config = auth_credential.service_account
92
- credentials = service_account.Credentials.from_service_account_info(
93
- config.service_account_credential.model_dump(), scopes=config.scopes
94
- )
95
-
96
- # Refresh credentials to ensure we have a valid access token
97
- credentials.refresh(Request())
98
-
99
- return AuthCredential(
100
- auth_type=AuthCredentialTypes.OAUTH2,
101
- google_oauth2_json=credentials.to_json(),
102
- )
103
- except Exception as e:
104
- raise ValueError(f"Failed to exchange service account token: {e}") from e