The code in PHP:
<?php
require 'C:\inetpub\wwwroot\Imzo.be\vendor\autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\inetpub\wwwroot\Imzo.be\secret\central-eon-267809-2d6dc3c388dc.json');
use Google\Cloud\Translate\V3\TranslationServiceClient;
function translate($text, $to){
$projectId = "central-eon-267809";
$translationClient = new TranslationServiceClient();
$string = $text;
$string = strip_tags(htmlspecialchars_decode($string));
$string = utf8_encode($string);
$content = [$string];
$targetLanguage = $to;
$response = $translationClient->translateText(
$content,
$targetLanguage,
TranslationServiceClient::locationName($projectId, 'global')
);
foreach ($response->getTranslations() as $key => $translation) {
$separator = $key === 2
? '!'
: ', ';
return $translation->getTranslatedText(); //. $separator;
}
}
?>
The Json-file (i'll make a new one when solved):
{
"type": "service_account",
"project_id": "central-eon-267809",
"private_key_id": "2d6dc3c388dc908c8fc440d238f0a9d237d0c2b9",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDPdczc7pvqh14I\nFJee/Ayhygn80nK6dKe8v6JZeMgfUx7e5/ziaV7oDufw/FAtkfRXa6YE7IMnmLeZ\nQ5hv3/PDEZ/Gs4QvBlaIGVAcN2Omi3k3eeC/osc1RzoVm7uG+eW9rjUe/XqtfVfV\nHhpnt5UbRR0uB0m4t/sIikprmS7gSL8ft80M7fYc2f7c9sQgkUlgwbYIogXVbxhl\n1Vym61PikgYnbAksZsHKEvRDdQx/+lzfvggMMeKlxw88DpFTIVyxctz/MkrY4Jid\n67Hj9L4A1Kcw981nDGGEefQ7yj3M2H9MLq6bGZs78Qv/dUi3nwcQaoHxKI592zrd\nfMFb9MLrAgMBAAECggEAILg2CsQ75VCF5Tqw3RtojJ3LrHWuhYy/exaIRESMijA3\nBrX08uqRKYKsVHF0Mx5kDeOa/VzQpKPtP6gNuIX22+sBgDpbhyUglvooYdg0iGr9\nP7bNBRG8Xxaa6Rc04NQJ8WaygcQ9nx4ih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g84v8R9PhnL3Ty6tTZXCAII\nr/kKDFm/VK3aUvpC92m5xfTE\n-----END PRIVATE KEY-----\n",
"client_email": "translate@central-eon-267809.iam.gserviceaccount.com",
"client_id": "100927717394460781967",
"auth_uri": "https://www.googleapis.com/auth/cloud-platform",
"token_uri": "https://www.googleapis.com/auth/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/translate%40central-eon-267809.iam.gserviceaccount.com"
}
The error:
[02-Apr-2021 15:49:52 Europe/Brussels] PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: 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."}
in C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113
Stack trace:
#0 C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\guzzle\src\Middleware.php(69): GuzzleHttp\Exception\RequestException::create()
#1 C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\promises\src\Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}()
#2 C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\promises\src\Promise.php(153): GuzzleHttp\Promise\Promise::callHandler()
#3 C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\promises\src\TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#4 C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\promises\src\Promise.php(248): GuzzleHttp\Promise\TaskQueue->run()
#5 C:\inetpub\wwwroot\Imzo.be\vendor\guzzleh in C:\inetpub\wwwroot\Imzo.be\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113
Does anyone know what is wrong in this? I think everything is configured correctly in the Google Cloud. It might be a problem with the auth_uri or token_uri. Please help.