【发布时间】:2019-10-22 06:42:58
【问题描述】:
我正在尝试在 Laravel (v ^6.0) 中使用 google/cloud-translate 库 (v ^1.5)。
在GoogleController.php:
public function translate(Request $request) {
$request->validate([
'source' => 'required|string|min:2|max:5',
'target' => 'required|string|min:2|max:5',
'q' => 'required|string',
]);
$translate = new TranslateClient([
'keyFile' => base_path(config('services.google.json_path')),
'projectId' => config('services.google.project_id'),
'suppressKeyFileNotice' => true,
]);
// Translate text from english to french.
$result = $translate->translate($request->q, [
'target' => explode($request->target, '-')[0],
'source' => explode($request->source, '-')[0],
]);
return $result;
}
但是在 Postman 中调用路由给了我错误:
Argument 2 passed to Google\Auth\CredentialsLoader::makeCredentials() must be of the type array, string given, called in /[...]/vendor/google/cloud-core/src/RequestWrapperTrait.php on line 155
我检查了 projectId 和 keyFile 的路径是否正确。任何人都可以阐明如何克服这个错误吗?
【问题讨论】:
标签: php google-cloud-platform google-api-php-client google-translate