Skip to content

Commit 8226303

Browse files
committed
feat(genai): Update bearer token code
1 parent 5ac397b commit 8226303

4 files changed

Lines changed: 39 additions & 20 deletions

genai/live/live_websocket_audiogen_with_txt.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919

2020
def get_bearer_token():
21-
command = "gcloud auth application-default print-access-token"
22-
result = subprocess.check_output(command, shell=True, text=True)
23-
return result.strip()
21+
import google.auth
22+
from google.auth.transport.requests import Request
23+
24+
creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
25+
auth_req = Request()
26+
creds.refresh(auth_req)
27+
bearer_token = creds.token
28+
return bearer_token
2429

2530

2631
# get bearer token
@@ -44,7 +49,7 @@ async def generate_content() -> str:
4449
PROJECT_ID = os.getenv("GOOGLE_SAMPLES_PROJECT")
4550
LOCATION = "us-central1"
4651
GEMINI_MODEL_NAME = "gemini-2.0-flash-live-preview-04-09"
47-
# To generate a bearer token, use:
52+
# To generate a bearer token in CLI, use:
4853
# $ gcloud auth application-default print-access-token
4954
# It's recommended to fetch this token dynamically rather than hardcoding.
5055
# BEARER_TOKEN = "ya29.a0AW4XtxhRb1s51TxLPnj..."

genai/live/live_websocket_audiotranscript_with_txt.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919

2020
def get_bearer_token():
21-
command = "gcloud auth application-default print-access-token"
22-
result = subprocess.check_output(command, shell=True, text=True)
23-
return result.strip()
21+
import google.auth
22+
from google.auth.transport.requests import Request
23+
24+
creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
25+
auth_req = Request()
26+
creds.refresh(auth_req)
27+
bearer_token = creds.token
28+
return bearer_token
2429

2530

2631
# get bearer token
@@ -44,7 +49,7 @@ async def generate_content() -> str:
4449
PROJECT_ID = os.getenv("GOOGLE_SAMPLES_PROJECT")
4550
LOCATION = "us-central1"
4651
GEMINI_MODEL_NAME = "gemini-2.0-flash-live-preview-04-09"
47-
# To generate a bearer token, use:
52+
# To generate a bearer token in CLI, use:
4853
# $ gcloud auth application-default print-access-token
4954
# It's recommended to fetch this token dynamically rather than hardcoding.
5055
# BEARER_TOKEN = "ya29.a0AW4XtxhRb1s51TxLPnj..."

genai/live/live_websocket_textgen_with_audio.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919

2020
def get_bearer_token():
21-
command = "gcloud auth application-default print-access-token"
22-
result = subprocess.check_output(command, shell=True, text=True)
23-
return result.strip()
21+
import google.auth
22+
from google.auth.transport.requests import Request
23+
24+
creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
25+
auth_req = Request()
26+
creds.refresh(auth_req)
27+
bearer_token = creds.token
28+
return bearer_token
2429

2530

2631
# get bearer token
@@ -39,23 +44,22 @@ async def generate_content() -> str:
3944
from websockets.asyncio.client import connect
4045
from scipy.io import wavfile
4146

42-
4347
def read_wavefile(filepath):
4448
# Read the .wav file.
4549
rate, data = wavfile.read(filepath)
4650
# Convert the NumPy array of audio samples back to raw bytes
4751
raw_audio_bytes = data.tobytes()
4852
# Encode the raw bytes to a base64 string.
4953
# The result needs to be decoded from bytes to a UTF-8 string
50-
base64_encoded_data = base64.b64encode(raw_audio_bytes).decode('ascii')
54+
base64_encoded_data = base64.b64encode(raw_audio_bytes).decode("ascii")
5155
mime_type = f"audio/pcm;rate={rate}"
5256
return base64_encoded_data, mime_type
5357

5458
# Configuration Constants
5559
PROJECT_ID = os.getenv("GOOGLE_SAMPLES_PROJECT")
5660
LOCATION = "us-central1"
5761
GEMINI_MODEL_NAME = "gemini-2.0-flash-live-preview-04-09"
58-
# To generate a bearer token, use:
62+
# To generate a bearer token in CLI, use:
5963
# $ gcloud auth application-default print-access-token
6064
# It's recommended to fetch this token dynamically rather than hardcoding.
6165
# BEARER_TOKEN = "ya29.a0AW4XtxhRb1s51TxLPnj..."
@@ -113,8 +117,8 @@ def read_wavefile(filepath):
113117
"parts": [
114118
{
115119
"inlineData": {
116-
"mimeType": mime_type, # Example value: "audio/pcm;rate=24000"
117-
"data": encoded_audio_message, # Example value: "AQD//wAAAAAAA....."
120+
"mimeType": mime_type, # Example value: "audio/pcm;rate=24000"
121+
"data": encoded_audio_message, # Example value: "AQD//wAAAAAAA....."
118122
}
119123
}
120124
],

genai/live/live_websocket_textgen_with_txt.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919

2020
def get_bearer_token():
21-
command = "gcloud auth application-default print-access-token"
22-
result = subprocess.check_output(command, shell=True, text=True)
23-
return result.strip()
21+
import google.auth
22+
from google.auth.transport.requests import Request
23+
24+
creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
25+
auth_req = Request()
26+
creds.refresh(auth_req)
27+
bearer_token = creds.token
28+
return bearer_token
2429

2530

2631
# get bearer token
@@ -40,7 +45,7 @@ async def generate_content() -> str:
4045
PROJECT_ID = os.getenv("GOOGLE_SAMPLES_PROJECT")
4146
LOCATION = "us-central1"
4247
GEMINI_MODEL_NAME = "gemini-2.0-flash-live-preview-04-09"
43-
# To generate a bearer token, use:
48+
# To generate a bearer token in CLI, use:
4449
# $ gcloud auth application-default print-access-token
4550
# It's recommended to fetch this token dynamically rather than hardcoding.
4651
# BEARER_TOKEN = "ya29.a0AW4XtxhRb1s51TxLPnj..."

0 commit comments

Comments
 (0)