我正在使用v1 API从Laravel web应用程序中的图像中读取文本。
但我必须在代币到期后生成代币。
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?
处理和管理身份验证凭据的最佳方法和建议是使用谷歌提供的官方工具。使用PHP的Auth库,然后将其与您使用的HTTP客户端集成。
云API的范围是:https://www.googleapis.com/auth/cloud-platform
例如:
use GoogleAuthApplicationDefaultCredentials;
use GuzzleHttpClient;
use GuzzleHttpHandlerStack;
// 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');