asp C# Application Default Credentials are not available
Asked Answered
P

5

11

I am running Google Translate API in C#. Running locally on my computer the next code works, but online on a server it throws the following error:

using Google.Cloud.Translation.V2;
TranslationClient client = TranslationClient.Create();
var response = client.TranslateText(sentence, targetLanguage, sourceLanguage: sourceLanguage);

"The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information."

Locally this runs just by installing Cloud SDK Installer which does all the settings, there is no need for authentication in code. On the server, should I use instead OAuth 2.0 or Service account keys ?

Can someone assist me on how to solve this?

EDIT: Can someone confirm to me if it is necessary to have access to the local server to run commands in command line like here https://cloud.google.com/storage/docs/authentication ? This would be pretty ridiculous, instead of just writing code. For example Youtube API does not require local access.

Psyche answered 18/7, 2017 at 11:10 Comment(0)
P
1

The easiest answer to my question , to avoid local settings on the server, is the third option of using the Translation API described below: using API keys. This means just a simple POST to an endpoint that has the API key in the link.

https://cloud.google.com/docs/authentication/#getting_credentials_for_server-centric_flow https://cloud.google.com/docs/authentication/api-keys

Psyche answered 19/7, 2017 at 12:15 Comment(1)
"This means just a simple POST to an endpoint that has the API key in the link." You don't need to do that - you can still use TranslationClient, but call TranslationClient.CreateFromApiKey. (That said, service accounts are now preferred over API keys.)Bortz
E
32

Follow directions to get json file:

https://cloud.google.com/translate/docs/reference/libraries

Then run this code first:

System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "c:\mypath\myfile.json");
Emelda answered 9/7, 2018 at 19:41 Comment(1)
why on earth they don't allow API key?Pantomimist
M
3

To generate a private key in JSON or PKCS12 format:

  1. Open the list of credentials in the Google Cloud Platform Console. OPEN THE LIST OF CREDENTIALS
  2. Click Create credentials.
  3. Select Service account key. A Create service account key window opens.
  4. Click the drop-down box below Service account, then click New service account.
  5. Enter a name for the service account in Name.
  6. Use the default Service account ID or generate a different one.
  7. Select the Key type: JSON or P12.
  8. Click Create. A Service account created window is displayed and the private key for the Key type you selected is downloaded automatically. If you selected a P12 key, the private key's password ("notasecret") is displayed.
  9. Click Close.

You can find more details here https://cloud.google.com/storage/docs/authentication

Misbeliever answered 20/7, 2017 at 19:26 Comment(0)
D
1

Its all in the errormessage. You have two options

  • Run the Google Compute Engine on the machine you have your program running on and input your credentials there.

  • Use a service account and set the "GOOGLE_APPLICATION_CREDENTIALS" environment variable to reference your credentials file (which is a .json file that you can download from the google developer console.)

PS: Do not store your credentials file anywhere on the server where it may be accessed by someone else!

Donoho answered 18/7, 2017 at 11:56 Comment(2)
thanks. unfortunately I cannot run the google cloud sdk from here cloud.google.com/sdk/docs on the server since it is a shared Windows server with other users. I could rent a Windows server instance online, but that would cost me monthly. do you have any other solutions?Psyche
As i tried to say in my second point. It is also possible to download your credentials from the Google developers console and set a Path variable on the windows system that points to this file. Or as amr answered below you could also insert the certificate in your code. Either way you do not have to install the cloud sdk.Donoho
M
1

You must download API key from

https://console.developers.google.com/iam-admin/serviceaccounts After that download .P12 file file to use it in your code

var certificate = new X509Certificate2(@"key3.p12", "notasecret", X509KeyStorageFlags.Exportable); notasecret is default password

Misbeliever answered 18/7, 2017 at 14:36 Comment(1)
thanks, I do not see in the console from where to download the P12. or how to use it. do you have a documentation link?Psyche
P
1

The easiest answer to my question , to avoid local settings on the server, is the third option of using the Translation API described below: using API keys. This means just a simple POST to an endpoint that has the API key in the link.

https://cloud.google.com/docs/authentication/#getting_credentials_for_server-centric_flow https://cloud.google.com/docs/authentication/api-keys

Psyche answered 19/7, 2017 at 12:15 Comment(1)
"This means just a simple POST to an endpoint that has the API key in the link." You don't need to do that - you can still use TranslationClient, but call TranslationClient.CreateFromApiKey. (That said, service accounts are now preferred over API keys.)Bortz

© 2022 - 2024 — McMap. All rights reserved.