Reputation: 412
I'm trying to use Google.Cloud.Translation service of Google Cloud platform (GCP) for the translation of text in Xamarin forms. I want to translate my application from English to Arabic. I enabled the API. Created a json file from the Service account. Followed the step in the following url https://cloud.google.com/translate/docs/setup
But still when I try to set the GOOGLE_APPLICATION_CREDENTIALS I get exception-
Error reading credential file from location
C:/Users/xxx/Downloads/xxxx-280817-bedb566bfd11.json: Could not find a part of the path "/C:/Users/xxxx/Downloads/xxxx-280817-bedb566bfd11.json". Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS
I have tried numerous ways to fix this. This is my code:
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS","C:/Users/xxx/Downloads/xx-280817-bedb566bfd11.json");
TranslationClient client = TranslationClient.Create();
How can I fix this, so I can have translation of English to Arabic language in my Xamarin form?
Upvotes: 1
Views: 1049
Reputation: 5109
The issue you are having is that 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 on your local machine, pointing to a file defining the credentials as shown here.
If you are using a Mac and you place the json file in the Downloads folder, you would open your Terminal app and run this command in it:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/xx-280817-bedb566bfd11.json"
It seems like you are using a Windows computer and you placed the file in your Downloads folder, so you would open your "Windows Command Prompt" (type CMD in Run, or search for Command Prompt) and run this command:
set GOOGLE_APPLICATION_CREDENTIALS="C:\Users\xxx\Downloads\xx-280817-bedb566bfd11.json"
Make sure that the xx-280817-bedb566bfd11.json is the correct file name of the JSON file that contains your service account key.
You could get the service account key in the Google Cloud Platform Console as shown here.
Upvotes: 1
Reputation: 607
Put your GOOGLE_APPLICATION_CREDENTIALS json file as EmbeddedResource and you can get yout path by this way
<ItemGroup>
<EmbeddedResource Include="_fonts/OpenSans.ttf" />
</ItemGroup>
to get the file path
var assembly = typeof(MyLibrary.MyClass).GetTypeInfo().Assembly;
Stream resource = assembly.GetManifestResourceStream("MyLibrary._fonts.OpenSans.ttf");
The key point is to use the right name on GetManifestResourceStream call. You have to use [assembly name].[directory].[file name]
.
Ref link: https://stackoverflow.com/a/41106851/4778712
Upvotes: 2