Omkar008 commited on
Commit
5078916
·
verified ·
1 Parent(s): 5f63c51

Update authenticate.py

Browse files
Files changed (1) hide show
  1. authenticate.py +44 -1
authenticate.py CHANGED
@@ -15,4 +15,47 @@ def get_access_token():
15
  if credentials.expired:
16
  credentials.refresh(Request())
17
 
18
- return credentials.token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  if credentials.expired:
16
  credentials.refresh(Request())
17
 
18
+ return credentials.token
19
+
20
+ def get_access_token_v1():
21
+ """
22
+ Generate a Google Cloud access token from service account credentials.
23
+ Returns the access token string or raises an exception if failed.
24
+ """
25
+ try:
26
+ # Set up logging
27
+ logging.basicConfig(level=logging.INFO)
28
+ logger = logging.getLogger(__name__)
29
+
30
+ # Get credentials from environment variable
31
+ creds_json = os.getenv('ACCOUNT_CREDS')
32
+ if not creds_json:
33
+ raise ValueError("ACCOUNT_CREDS environment variable not found")
34
+
35
+ # Parse credentials JSON
36
+ try:
37
+ creds_dict = json.loads(creds_json)
38
+ except json.JSONDecodeError:
39
+ raise ValueError("Invalid JSON in ACCOUNT_CREDS")
40
+
41
+ # Create credentials object
42
+ credentials = service_account.Credentials.from_service_account_info(
43
+ creds_dict,
44
+ scopes=['https://www.googleapis.com/auth/cloud-platform']
45
+ )
46
+
47
+ # Ensure token is valid and refresh if needed
48
+ if not credentials.valid:
49
+ logger.info("Token expired or invalid, refreshing...")
50
+ credentials.refresh(Request())
51
+
52
+ if not credentials.token:
53
+ raise ValueError("No token generated after refresh")
54
+
55
+ logger.info("Successfully generated access token")
56
+ return credentials.token
57
+
58
+ except Exception as e:
59
+ logger.error(f"Error generating access token: {str(e)}")
60
+ raise
61
+