Hi,
I am using vertexai agentbuilder to parse pdf files and get processed chunks. For this I need to send HTTP request to discoveryengine (v1 alpha) API. It is not possible to use a python client for this. Here is the code I am using:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://discoveryengine.googleapis.com/v1alpha/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/branches/0/documents/DOCUMENT_ID/chunks:getProcessedDocument?processed_document_type=CHUNKED_DOCUMENT"
Link: https://cloud.google.com/generative-ai-app-builder/docs/parse-chunk-documents#list-chunks
This code works well when I use it locally. Here is the code I use for this:
credentials = service_account.Credentials.from_service_account_file(
find_dotenv('my-google-service-key.json'),
scopes=['https://www.googleapis.com/auth/cloud-platform']
)
credentials.refresh(Request())
print("Credentials loaded from local file")
# Make the request with params
headers = {
'Authorization': f'Bearer {credentials.token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers, params=params)
I couldn’t manage to use the same code if used within an fastapi app deployed on GoogleCloudRun. I upload my service account key json file to SecretManager. And then within GoogleCloudRun app I use the following code to do that same thing:
client = secretmanager.SecretManagerServiceClient()
name = f"projects/{project_id}/secrets/ServiceAccountKey/versions/latest"
response = client.access_secret_version(request={"name": name})
credentials = service_account.Credentials.from_service_account_info(
json.loads(response.payload.data),
scopes=['https://www.googleapis.com/auth/cloud-platform'] # Added required scope
)
try:
credentials.refresh(Request())
credentials_error="No error for credentials"
except Exception as e:
credentials_error=e
print(f"Error refreshing credentials: {e}")
headers = {
'Authorization': f'Bearer {credentials.token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers, params=params)
But this gives me the following error:
"detail": "Error: API request failed: 401 - {\n \"error\": {\n \"code\": 401,\n \"message\": \"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.\",\n \"status\": \"UNAUTHENTICATED\",\n \"details\": [\n {\n \"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\",\n \"reason\": \"ACCESS_TOKEN_TYPE_UNSUPPORTED\",\n \"metadata\": {\n \"method\": \"google.cloud.discoveryengine.v1alpha.DocumentService.GetProcessedDocument\",\n \"service\": \"discoveryengine.googleapis.com\"\n }\n }\n ]\n }\n}\n"
How can I successfully send HTTP request to discovery engine API when the app is deployed on GoogleCloudRun?