AutoML php PredictionServiceClient 身份验证错误

时间:2021-02-12 21:56:08

标签: php google-cloud-platform google-cloud-automl automl

我正在尝试将 Google AutoML 预测服务与我训练的自定义模型一起使用,但它返回以下错误:

Client error: `POST https://oauth2.googleapis.com/token` resulted in a `400 Bad Request` response:
{"error":"invalid_scope","error_description":"Invalid OAuth scope or ID token audience provided."}

我正在使用以下代码,类似于文档:

use Google\Cloud\AutoMl\V1\ExamplePayload;
use Google\Cloud\AutoMl\V1\Image;
use Google\Cloud\AutoMl\V1\PredictionServiceClient;

putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json");

$service = new PredictionServiceClient();
        try {
            $formattedName = $service->modelName('project-name', 'region', 'model');
            $content = file_get_contents($filePath); //defined in other side as the path to the photo
            $image = (new Image())->setImageBytes($content);
            $payload = (new ExamplePayload())->setImage($image);
            $params = ['score_threshold' => '0.5']; // value between 0.0 and 1.0
            $response = $service->predict($formattedName, $payload, $params);
            $annotations = $response->getPayload();
            foreach ($annotations as $annotation) {
                $spaceName = $annotation->getDisplayName();
            }
        } finally {
            $service->close();
        }

我在部署模型后尝试使用google提供的curl,结果如下:

   {
      "error": {
        "code": 401,
        "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.",
        "status": "UNAUTHENTICATED"
      }
    }

本例中使用的代码是:

         putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json");
         $file = file_get_contents('/path/to/photo.jpg');
         $image = base64_encode($file);
         $url = "https://automl.googleapis.com/v1/projects/[project_name]/locations/[region]/models/[model]:predict";

         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_POST, true);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

         $headers = [
         "Content-Type: application/json",
         "Authorization: Bearer $(gcloud auth application-default print-access-token)",
         ];
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

         $data = '{"payload":{"image":{"imageBytes": "' . $image . '"}}}';
         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         //for debug only!
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

         $resp = curl_exec($curl);
         curl_close($curl);

我已经阅读了有关如何获取凭据的所有文档,并且都在 key.json 文件中。

有人知道我需要什么才能做出成功预测吗?提前致谢!!

1 个答案:

答案 0 :(得分:1)

可能是因为返回了 Oauth 错误,所以 putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json"); 没有生效。相反,您可以在代码中删除它并在运行脚本之前设置环境变量。只需确保您使用的是正确的服务帐户路径即可。

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"

我使用您的代码对此进行了测试,并且只添加了名称拼写。我使用了 AutoML Vision quick start 中检测花朵的数据集。

use Google\Cloud\AutoMl\V1\ExamplePayload;
use Google\Cloud\AutoMl\V1\Image;
use Google\Cloud\AutoMl\V1\PredictionServiceClient;

$filePath = '/my_file_path/red-rose.jpg';
$service = new PredictionServiceClient();
        try {
            $formattedName = $service->modelName('my-project', 'us-central1', 'my-model-id');
            $content = file_get_contents($filePath); //defined in other side as the path to the photo
            $image = (new Image())->setImageBytes($content);
            $payload = (new ExamplePayload())->setImage($image);
            $params = ['score_threshold' => '0.5']; // value between 0.0 and 1.0
            $response = $service->predict($formattedName, $payload, $params);
            $annotations = $response->getPayload();
            foreach ($annotations as $annotation) {
                    $spaceName = $annotation->getDisplayName();
                    printf('Predicted class name: %s' . PHP_EOL, $spaceName);
            }
        } finally {
            $service->close();
        }

测试: enter image description here

相关问题