Search code examples
.netgoogle-apilabviewgoogle-translation-api

How can I set Google API Key programmatically? I'm getting "The request is missing a valid API key." Error (403)


I am accessing the .NET DLL in LabVIEW for the google translation API (Google.Cloud.Translation.V2.dll), and tried to set the "GOOGLE_APPLICATION_CREDENTIALS" via the command prompt as shown in Google's example on how to set everything up to get the translate API working. It does not seem to work and when I run my code, I get a "missing a valid API key" error (403). I think I need to set the key programmatically but I cannot find any documentation or examples anywhere on how to set the API Key programmatically. Below is the .NET error I received when trying to translate some German text to English.

Google.GoogleApiException: Google.Apis.Requests.RequestError
The request is missing a valid API key. [403]
Errors [
Message[The request is missing a valid API key.] Location[ - ] Reason[forbidden] Domain[global]
]

Do I need access to a different DLL to set the API Key? I downloaded all of these (because they were dependencies and I wasn't sure what all I needed when accessing the Google API in LabVIEW) and have looked through potential methods I could use in all of them. These are them:

Google.Cloud.Translation.V2.dll, Google.Apis.Translate.v2.dll, Google.Api.Gax.dll, Google.Api.Gax.Rest.dll, Google.Apis.Auth.dll, Google.Apis.Auth.PlatformServices.dll, Google.Apis.Core.dll, Google.Apis.dll, Google.Apis.PlatformServices.dll, Newtonsoft.Json.dll, System.Interactive.Async.dll


Solution

  • I accomplished setting the "GOOGLE_APPLICATION_CREDENTIALS" system environment variable in C# with the following code:

    string keyFilepath = @"JSON\Key\FilePath\Here";
    System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", keyFilepath);
    
    

    I accomplished this same code in LabVIEW with the following code.

    https://i.sstatic.net/QaKBY.jpg

    The second window shows the selected .NET constant being passed into the first invoke node. Create a constant reference passed into the invoke node, right click and select class, browse to C:\WINDOWS\Microsoft.NET\Framework\ and select your latest version of Framework on your PC, and dive into mscorlib.

    I accomplished the German Translation with the following LabVIEW code, the right window being the selected .NET constant being passed into the first invoke node. https://i.sstatic.net/lufVu.jpg