1717
1818import base64
1919import importlib
20+ import logging
2021import threading
2122import time
22- import logging
2323from abc import ABC , abstractmethod
24+ from functools import cached_property
2425from typing import Any , Dict , List , Optional , Type
2526
2627import requests
@@ -158,16 +159,19 @@ def __init__(
158159 self ._expires_at = 0
159160 self ._lock = threading .Lock ()
160161
162+ @cached_property
163+ def _client_secret_header (self ) -> str :
164+ creds = f"{ self .client_id } :{ self .client_secret } "
165+ creds_bytes = creds .encode ("utf-8" )
166+ b64_creds = base64 .b64encode (creds_bytes ).decode ("utf-8" )
167+ return f"Basic { b64_creds } "
168+
161169 def _refresh_token (self ) -> None :
162- data = {
163- "grant_type" : "client_credentials" ,
164- "client_id" : self .client_id ,
165- "client_secret" : self .client_secret ,
166- }
170+ data = {}
167171 if self .scope :
168172 data ["scope" ] = self .scope
169173
170- response = requests .post (self .token_url , data = data )
174+ response = requests .post (self .token_url , data = data , headers = { "Authorization" : self . _client_secret_header } )
171175 response .raise_for_status ()
172176 result = response .json ()
173177
@@ -177,11 +181,11 @@ def _refresh_token(self) -> None:
177181 raise ValueError (
178182 "The expiration time of the Token must be provided by the Server in the Access Token Response in `expires_in` field, or by the PyIceberg Client."
179183 )
180- self ._expires_at = time .time () + expires_in - self .refresh_margin
184+ self ._expires_at = time .monotonic () + expires_in - self .refresh_margin
181185
182186 def get_token (self ) -> str :
183187 with self ._lock :
184- if not self ._token or time .time () >= self ._expires_at :
188+ if not self ._token or time .monotonic () >= self ._expires_at :
185189 self ._refresh_token ()
186190 if self ._token is None :
187191 raise ValueError ("Authorization token is None after refresh" )
@@ -211,6 +215,8 @@ def __init__(
211215
212216 def auth_header (self ) -> str :
213217 return f"Bearer { self .token_provider .get_token ()} "
218+
219+
214220class GoogleAuthManager (AuthManager ):
215221 """An auth manager that is responsible for handling Google credentials."""
216222
0 commit comments