Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .net MVC, set path to Google Application Credentials JSON file

I've just got this Google Sample Project to work on my VS2015,

However, after I published to "IIS" and host it, when I opened the link, the web page kept showing this message as the picture showed

Error Picture

I added the key to the web.config, but still doesn't work, just wondering if anyone had done this before, I really need help, thank you!

Edit: Here is my code

Web.Config

  <appSettings>
<!-- Set to your Google project id as shown on the Google Developers Console -->
<add key="GoogleCloudSamples:ProjectId" value="gdtest-1332" />
<add key="GOOGLE_APPLICATION_CREDENTIALS" value="D:\ProjectCloud\gdtest-12323.json" />
<!--
Set to either mysql or datastore.
If using mysql, update the connectionString far below, and then run Update-Database in the
Package Manager Console.
-->
<add key="GoogleCloudSamples:BookStore" value="datastore" />
<!-- Set to your Google Cloud Storage bucket -->
<add key="GoogleCloudSamples:BucketName" value="cloudstoragetestbillez" />
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

like image 351
Steven Wang Avatar asked Jun 04 '16 03:06

Steven Wang


Video Answer


2 Answers

Programmatically set an environment variable at runtime as such:

string credential_path = @"C:\..\key.json";
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);

I'm not an expert with IIS but it won't seem to pick up on environment variable set in your desktop environment.

like image 51
Danielle Cerisier Avatar answered Oct 18 '22 21:10

Danielle Cerisier


Restarting the Visual Studio 2017 did it for me for Console Application.

Console.WriteLine(System.Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"));

like image 42
Kunal B. Avatar answered Oct 18 '22 20:10

Kunal B.