Skip to content

Instantly share code, notes, and snippets.

@epynic
Created May 12, 2019 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epynic/3f20f2c1e8acdcd3bea8f8b4bd388c13 to your computer and use it in GitHub Desktop.
Save epynic/3f20f2c1e8acdcd3bea8f8b4bd388c13 to your computer and use it in GitHub Desktop.
CURL sample request to AutoML REST API
<?php
$image = $_POST['image'];
$key = trim(shell_exec('sudo -u service_name gcloud auth application-default print-access-token 2<&1')); // replace service_name with your service account name
$post='{"payload":{"image":{"imageBytes": "'.$image.'"}}}';
$curl = curl_init();
// The REST API URL can be found from the predict tab example replace with your account URL https://automl.googleapis.com/v1beta1/projects/quizappflutter/locations/us-central1/models/ICNXXXXXXXXXX41:predict
curl_setopt_array($curl, array(
 CURLOPT_URL => "https://automl.googleapis.com/v1beta1/projects/quizappflutter/locations/us-central1/models/ICNXXXXXXXXXX41:predict",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30, 
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => $post,
 CURLOPT_HTTPHEADER => array(
 "Authorization: Bearer $key",
 "Content-Type: application/json",
 ), 
));
$response = curl_exec($curl);
$err = curl_error($curl); 
 
curl_close($curl);
 
if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}
@dre-dev
Copy link

dre-dev commented Jan 22, 2021

I've tried this code and change the service name and the url but the output says,
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

@epynic
Copy link
Author

epynic commented Jan 22, 2021

Can you check the $key variable outout from $key = trim(shell_exec('sudo -u service_name gcloud auth application-default print-access-token 2<&1'));

@epynic
Copy link
Author

epynic commented Jan 22, 2021

The above code should work on a google-instance with all the necessary permissions service account permission given https://link.medium.com/PbL3avfQCW

@dre-dev
Copy link

dre-dev commented Jan 22, 2021

According to what I did for this case. In order to able access the curl via ssh or terminal, you must run this code below,
export GOOGLE_APPLICATION_CREDENTIALS="key-path.json"

Then I tried to run it in php with your code. I did all your steps in medium, and even try all methods i get from google. The only thing i get from the output since a week ago until now '"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. '

@epynic
Copy link
Author

epynic commented Jan 22, 2021

not sure but it should work, you have also activated your service account right ?

gcloud auth activate-service-account --key-file=key.json
https://stackoverflow.com/questions/43278622/gcloud-auth-activate-service-account-error-please-ensure-provided-key-file-is

@mvdbergh
Copy link

Hi there, anyone tried this in production on a live server?
I'm battling to get the token, my error: "Uncaught Error: Object of class Google\Cloud\Core\ServiceBuilder could not be converted to string"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment