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
+ # The tokens obtained by these functions are formatted as "Bearer" tokens
16
+ # and are intended to be passed in the "Authorization" header of HTTP requests.
17
+ #
18
+ # Example User Experience:
19
+ # from toolbox_core import toolbox_auth_methods
20
+ #
21
+ # auth_token_provider = toolbox_auth_methods.aget_google_id_token
22
+ # toolbox = ToolboxClient(
23
+ # URL,
24
+ # client_headers={"Authorization": auth_token_provider},
25
+ # )
26
+ # tools = await toolbox.load_toolset()
27
+
28
+
1
29
from functools import partial
2
30
3
31
import google .auth
8
36
9
37
10
38
async def aget_google_id_token ():
39
+ """
40
+ Asynchronously fetches a Google ID token.
41
+
42
+ The token is formatted as a 'Bearer' token string and is suitable for use
43
+ in an HTTP Authorization header. This function uses Application Default
44
+ Credentials.
45
+
46
+ Returns:
47
+ A string in the format "Bearer <google_id_token>".
48
+ """
11
49
creds , _ = default_async ()
12
50
await creds .refresh (_aiohttp_requests .Request ())
13
51
creds .before_request = partial (Credentials .before_request , creds )
@@ -16,6 +54,16 @@ async def aget_google_id_token():
16
54
17
55
18
56
def get_google_id_token ():
57
+ """
58
+ Synchronously fetches a Google ID token.
59
+
60
+ The token is formatted as a 'Bearer' token string and is suitable for use
61
+ in an HTTP Authorization header. This function uses Application Default
62
+ Credentials.
63
+
64
+ Returns:
65
+ A string in the format "Bearer <google_id_token>".
66
+ """
19
67
credentials , _ = google .auth .default ()
20
68
session = AuthorizedSession (credentials )
21
69
request = Request (session )
0 commit comments