I have trained a model with AutoML with google cloud. I am currently working on a website POC Test, that take user inputs for each factor, I will connect to the trained model API and output a prediction.
Everything is written in html and javascript with ajax to make the API Calls.
I am facing problems with connecting the web to make valid REST API Calls.
To Clarify, I am finishing the whole API call and display result directly on the user's website.
Due to resources issue, I am not allowed to write a backend server handling the metadata.
Here is a snippet of the code. Credentials are masked.
const PROJECT_ID = "ProjectID" ;
const ENDPOINT_ID = "ENDPOINTID" ;
const ACCESS_TOKEN = "PRINTED FROM GCLOUD-AUTH-PRINT-ACCESS-TOKEN IN SHELL" ;
const ENDPOINTURL = `https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/endpoints/${ENDPOINT_ID}:predict` ;
Below is my ajax call:
$.ajax(
{
type : "POST",
url : ENDPOINTURL,
headers:
{
'Authorization': `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'application/json'
},
data: JSON.stringify(INPUT_DATA),
dataType: "json",
success: function(data)
{
handlePredictionResults(data);
},
error :function(xhr, status, error)
{
try
{
console.log("An error occurred:", error);
} catch (e)
{
console.log("An error occurred:", e);
}
}
});
I have been receiving two error code 401 and 404 upon inspecting the browser preview.
Which I know should indicate that I have authorisation issues and resource loading issues.
I wonder if I should use a Service Account / OAuth 2.0 token for connecting, but I do not see that inside the CURL REST API Request command on the console when I trained the model.
As mentioned in the comments by @John Hanley, Since you're unable to use backend at the moment you can make use of Google user OAuth .
For the OAuth 2.0 flow, you can follow this google documentation.
However, it is advisable to use service accounts for authorization which can be easily done from backend code.
Posting the answer as community wiki for the benefit of the community that might encounter this use case in the future. Feel free to edit this answer for additional information.