Google Vision API Invalid operation ID format

Viewed 46

I've been using the Google Vision API for a while now to extract text from documents (PDFs) but just came across an issue. I have created a long running job and now I need to check the job status. According to the documentation the GET request should be;

GET https://vision.googleapis.com/v1/operations/operation-id

However when trying that I get a response;

{ "error": { "code": 400, "message": "Invalid operation id format. Valid format is either projects/*/operations/* or projects/*/locations/*/operations/*", "status": "INVALID_ARGUMENT" } }

Ok, no problem, so I look through the docs and according to the message I should be able to do the following;

https://vision.googleapis.com/v1/projects/project-id/operations/1efec2285bd442df

Or;

https://vision.googleapis.com/v1/projects/project-id/locations/location-id/operations/1efec2285bd442df

My final code is a GET request using PHP Curl like so;

    $url = "https://vision.googleapis.com/v1/projects/myproject-id/operations/longrunningjobid";

        // create a curl request
        $ch = curl_init($url);

        // define the parameters
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization:Bearer $token", "Content-Type: application/json; charset=utf-8"));

        // execute the request
        $result = curl_exec($ch);

        // close the connection
        curl_close($ch);

echo $result;

I have tried several combinations of the url to try and get this to work. My gcp project id is correct and the job number is correct but I feel the url is not right. Any ideas?

1 Answers

The implementation was correct, however I was using regex earlier in the code and didn't realize that in PHP the \n character is escaped differently than in javascript.

So in javascript I was using \\n to escape it but in PHP I needed to use \\\n.

This was causing the longrunningjobid to have one too many characters.

Related