【问题标题】:Authentication for Google Document AI's v1 API in Laravel在 Laravel 中对 Google Document AI 的 v1 API 进行身份验证
【发布时间】:2021-07-19 18:49:52
【问题描述】:

我正在使用 v1 API 从 Laravel 网络应用中的图像中读取文本。

但我必须在它们过期后生成令牌。

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID:method

在 Laravel 中是否有其他更好的方式对 Google Cloud API 进行身份验证?

【问题讨论】:

  • 如果您在 Google Cloud 中运行 Laravel 应用程序,那么首选方法是使用 ADC(它将选择默认服务帐户)。您可以使用 Google Cloud SDK for PHP 或 Laravel 软件包之一,例如 superbalist/laravel-google-cloud-storage。该 Laravel 包也集成到 Laravel 文件系统中。
  • Laravel 应用程序位于 Microsoft Azure 中。
  • 哪个版本的 Laravel 以及 Azure 上的哪个服务。细节通常会对获得好的/正确答案产生很大影响。
  • Laravel 版本 8 和 Azure 上的应用服务。

标签: laravel azure google-cloud-platform google-authentication cloud-document-ai


【解决方案1】:

处理和管理身份验证凭据的最佳方式和recommended 是使用 Google 提供的官方工具。使用 Auth Library for PHP ,然后将其与您使用的 HTTP 客户端集成。

Cloud API 的作用域是:https://www.googleapis.com/auth/cloud-platform

例如:


use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

// specify the path to your application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');

// define the scopes for your API call
$scopes = ['https://www.googleapis.com/auth/cloud-platform'];

// create middleware
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
$stack = HandlerStack::create();
$stack->push($middleware);
// create the HTTP client
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://LOCATION-documentai.googleapis.com',
'auth' => 'google_auth' // authorize all requests
]);

// make the request
$response = $client->get('v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID:method');

【讨论】:

  • 此答案适用于 Laravel 8 的一项更改。Google auth 库不支持 Guzzle 7。将 composer.json 更改为 "guzzlehttp/guzzle": "^6.5|^7.0.1",
猜你喜欢
  • 2023-01-18
  • 1970-01-01
  • 2018-05-28
  • 2013-06-24
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 2019-06-03
相关资源
最近更新 更多