Resolving ‘Application Default Credentials’ error encountered while accessing GCP resources via ASP.NET applications

Introduction
When working with Google ASP.NET MVC 4 application or C# libraries, in order to leverage Google Cloud Platform compute infrastructure as Platform, most of user faces a common error, ‘The Application Default Credentials are not available’ or ‘the environment variable,
GOOGLE_APPLICATION_CREDENTIALS must be defined ‘.

The only reason for this error is, we need to authorize our application to our own Google Project.

Let’s understand and fix this with an example.

Prerequisites:
1) Google Cloud Platform Account - (You can have have your subscription, by clicking, (https://goo.gl/e5KNLF)

2) Google Cloud SDK - Download at https://goo.gl/Sxz6h8

Resolving with example
Now, create a new Google ASP.NET 4 MVC application, and provide it a suitable name as shown below.

Click Ok.


Select Google Cloud Platform project that will run this application. You can enter project ID or select from existing project being listed.

Once selected Click Ok.



This will create a basic Google ASP.NET MVC application structure, as seen in below image.



Now, let’s build & run the application. You will encounter with an error at filterConig.cs file.



A readable image of Error,



So as mentioned above this error occurs as we need to authorize application to Google Project.

Google Application Default Credentials
The preferred or the most recommended option to achieve this is to leverage Google Application Default Credentials via gcloud utility.

The gcloud Utility is part of Google Cloud SDK. While installing Cloud SDK, make sure you check entry mentioning Beta Component, to have this component installed in machine. Reason, command used to configure Google Application Default Credentials are still in beta. (At the time of writing this article).

If not selected, no worries, we can still have it through running Google Cloud Shell command,
gcloud components install beta
This command will install beta components manually.

Now open Google Cloud SDK Shell, being installed in your machine. And make sure you open it with
‘Run as administrator’.


Now run below beta command, as shown,
gcloud beta auth application-default login

This command will open browser to initiate authentication process.


Select and Login with google credentials associated with your project



And Click on Allow. This will authorize our machine to access projects via the credentials we opted for. So best part, it’s a onetime activity, and we need not require to repeat this again n again for different projects.



Once done, it will lead to below page mentioning authentication success.



Now the machine is setup to access Google Cloud Platform resources, being enabled for application.
File with associated credentials is downloaded in our machine.
Refer below image, it mentions the file details.


Once done with this process, now run the application.
Now it will work fine!


Above Setup will not only fix the error, but will make you ease to deploy application to either Google App Engine and Google Cloud Engine, without managing any config files or settings for different environments., as both the services are configured to use same credentials.

Revoking Credentials
You must be thinking, we have authorize entire machine to use our credential. What if we want to deauthorize or revoke this credential?

Well revoking this credentials is as simpler as it was adding.

Just run below command in Cloud Shell,
gcloud auth application-default revoke
It would prompt for continuing the revoke process, enter ‘Y’


Once the revoke is done, it will display message saying ‘Credentials Revoked’. This will delete the earlier downloaded credential file. This revoke will not affect any environment variables, or any other active account credentials on Google VM.



Now once again try running Google ASP.NET MVC application we created.
And it will throw error, confirming credentials are revoked!


So with few easy steps using gcloud, we authenticated our Google ASP.NET MVC C# application with default credentials. Also using the same we revoked credentials. Though there are another method to add this credential, but using above steps is simplest with most recommended option.

Chirag thumar is working as a web programmers at Nex, Nex is leading ASP.NET developmemnt company in India and USA. He runs his own company main focus Java based web application using JQuery, Struts, spring, Ext Js, etc. He Writes about Emergency technology, Java based frameworks & tools, Innovative quotes, Social Media News and online marketing.