Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AutoML php PredictionServiceClient自动验证错误_Php_Google Cloud Platform_Google Cloud Automl_Automl - Fatal编程技术网

AutoML php PredictionServiceClient自动验证错误

AutoML php PredictionServiceClient自动验证错误,php,google-cloud-platform,google-cloud-automl,automl,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

我尝试将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文件


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

可能是
putenv(“GOOGLE_应用程序_凭证=/path/to/key.json”)未生效,因为返回了Oauth错误。相反,您可以在代码中删除它,并在运行脚本之前设置环境变量。只需确保您使用的是正确的服务帐户路径

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
我用你的代码测试了这个,只添加了名字的priting。我使用了检测花朵的数据集

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();
        }
测试:

谢谢您的回答!我移除了putenv();从我的代码和错误仍然存在。我已经通过linux命令导出了凭据,并遵循了来自的所有建议。此外,mi服务帐户还具有automl所有者角色。我尝试更改到另一个服务帐户,结果相同。这里我遗漏了一些东西,你对这个案例还有其他建议吗?关于AutoML的任何操作都有同样的问题,不仅仅是预测。部署、取消部署、更新节点等。我将服务帐户导出到UbuntuEnv变量中,就像google推荐的那样,并尝试在此处注释php代码,我有“错误描述”:“提供的OAuth作用域或ID无效”。上面显示的测试是在CloudShell中完成的。你能试着在CloudShell中运行你的代码吗?另外,如果您使用IDE来运行PHP代码,那是什么IDE?我使用的是PHPStormV2019.3。如果我在控制台上运行curl,我得到了一个成功的预测,但是错误仍然存在于两个代码上,问题中都有错误。用卷曲预测对我来说也很有效。