I'm encountering an issue with authenticating my .NET MAUI project with the Google Cloud Translation API. I've set up a service account and obtained the necessary credentials in a JSON file. However, when I set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to this JSON file and attempt to authenticate with the Translation API, I keep receiving an error of "The service translate has thrown an exception. HttpStatusCode is BadRequest. API key not valid. Please pass a valid API key."
Here are the steps I've taken so far:
- Checked the API key is valid, saved the JSON file and added it to the environment variables, as shown below. enter image description here the JSON file is like this: enter image description here
Implemented code in my .NET MAUI project to authenticate with the Google Cloud Translation API using the Google.Cloud.Translation.V2 package.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TranslationDemo.Models;
using Google.Cloud.Translation.V2;
namespace TranslationDemo.ViewModels
{
public class MainPageViewModel : BindableObject
{
private string _helloWorldText = "Hello World";
public string HelloWorldTRRText
{
get => _helloWorldText;
set
{
_helloWorldText = value;
OnPropertyChanged(nameof(HelloWorldText));
}
}
public List<Lang> SupportedLanguages { get; } = new List<Lang>
{
new Lang { Code = "ja", Name = "Japanese" },
new Lang { Code = "de", Name = "German" },
new Lang { Code = "fr", Name = "French" }
// Add more languages as needed
};
private Lang _selectedLanguage;
public Lang SelectedLanguage
{
get => _selectedLanguage;
set
{
_selectedLanguage = value;
TranslateHelloWorld();
}
}
private async void TranslateHelloWorld()
{
// Implement translation logic using Google Cloud Translation API
// Set the translated text to HelloWorldText
// Simulate translation by appending the language code
//HelloWorldText = $"{_helloWorldText} ({_selectedLanguage.Code})";
TranslationService ts = new TranslationService("[MY_VAILID_KEY]");
HelloWorldText = ts.TranslateText(_helloWorldText, _selectedLanguage.Code);
}
}
public class TranslationService
{
private readonly TranslationClient _client;
public TranslationService(string apiKey)
{
_client = TranslationClient.CreateFromApiKey(apiKey);
}
public string TranslateText(string text, string targetLanguage)
{
try
{
var response = _client.TranslateText(text, targetLanguage);
return response.TranslatedText;
}
catch (Exception ex)
{
// Handle error, the execution always lands here!
return $"Error translating text: {ex.Message}";
}
}
}
}
Despite following these steps, I'm still encountering the "Invalid Key" error. I've verified that the JSON file contains the correct credentials and that the environment variable is set correctly. I've also restarted Visual Studio to ensure that the changes take effect.
Can anyone provide insights into what might be causing this issue and how I can resolve it? Any suggestions or troubleshooting steps would be greatly appreciated. Thank you!