Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Translate API

Hello,

I'm trying to create Glossary resources using the PHP facade and authenticating with a service account credential. I downloaded a JSON file and stored it in the project directory for authentication. However, I'm encountering an error that says: "Error creating glossary: { "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.", "code": 16, "status": "UNAUTHENTICATED", "details": [] }"

Interestingly, when I trigger the function, it successfully creates the glossaries. When I attempt to retrieve the glossaries by their ID, I receive the correct response with the expected data. If I trigger the function twice, I receive an error stating that the file name already exists. This inconsistency confuses me, as I expect to receive the project name and other details instead.

So my doubt is that can I use service account for creating the glossaries resources. or I have to use OAuth 2.0?


 

public function uploadCSV(Request $request) { 

$projectNumberOrId = env('GOOGLE_CLOUD_PROJECT_ID'); 

$glossaryId = env('GOOGLE_CLOUD_GLOSSARY_ID'); 

$bucketName = env('GOOGLE_CLOUD_BUCKET_NAME'); 

$glossaryFilename = 'glossary.csv'; // Set up authentication 

putenv('GOOGLE_APPLICATION_CREDENTIALS=path/kwy_file_name.json'); // Instantiate a client 

$translationServiceClient = new TranslationServiceClient(); // Designate the data center location that you want to use 

$location = 'us-central1'; // Set glossary resource name 

$name = $translationServiceClient->glossaryName($projectNumberOrId, $location, $glossaryId);

$languages = ['zh', 'th', 'tl']; // Set language codes 

$languageCodesSet = new LanguageCodesSet(); 

$languageCodesSet->setLanguageCodes($languages); 

$gcsSource = new GcsSource();
$gcsSource->setInputUri('gs://bucketName/glossary.csv');

 $inputConfig = new GlossaryInputConfig();
 $inputConfig->setGcsSource($gcsSource); // Set glossary resource information 

$glossary = new Glossary();
$glossary->setName($name); 
$glossary->setLanguageCodesSet($languageCodesSet);
$glossary->setInputConfig($inputConfig); 

$parent = TranslationServiceClient::locationName($projectNumberOrId, $location); // Create glossary resource 

// Handle exception for the case in which a glossary with glossary_name already exists try { 
$operationResponse = $translationServiceClient->createGlossary($parent, $glossary); 

$operationResponse->pollUntilComplete();
 if ($operationResponse->operationSucceeded())
 {
 // Retrieve the created glossary 
$response = $operationResponse->getResult(); 
printf('Created Glossary.' . PHP_EOL); 
printf('Glossary name: %s' . PHP_EOL, $response->getName()); 
printf('Entry count: %s' . PHP_EOL, 
$response->getEntryCount());
printf('Input URI: %s' . PHP_EOL, 
$response->getInputConfig()->getGcsSource()->getInputUri()); 
} else { 
$error = $operationResponse->getError(); // Handle error } 
} catch (\Google\ApiCore\ApiException $e) {
 // Handle API exception 
echo "Error creating glossary: " . $e->getMessage(); 
} finally { 
$translationServiceClient->close(); 
} 
} 

Response: Error creating glossary: { "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.", "code": 16, "status": "UNAUTHENTICATED", "details": [] }

 

 

1 1 739
1 REPLY 1
Top Labels in this Space
Top Solution Authors