Generate an API Key

Create a project

We will need to create a project in the Google Developer Console and create an API Key which we can use to call the Google Places API. Ensure that you have signed into your Google account and head over to https://console.developers.google.com/projectselector/apis/library. Click on the button to Create a project.

Give the project a name of Airport Explorer and then click on Create:

Once the project has been created, search for google places, and then click to select Google Places API Web Service:

Next, click on the button to Enable this service:

Create credentials

Once the service is enabled, click on Create Credentials:

Click on What credentials do I need?:

This will create an API Key for you. Copy the API key, as you will need it to call the Google Places API. Finally, you can click on the Done button.

Storing Secrets

Since the Google API Key is secret, we should not be storing it in our source code, and also not in one of our configuration files which may be checked into our source control system at a later stage. Thankfully ASP.NET Core gives us an easy way to manage secrets in our application and allow us to access them through the normal ASP.NET Core configuration service.

In Visual Studio, right-click on the project in the Solution Explorer window, and then select Manage User Secrets….

This will open a secrets.json file which is stored in a secure location on your hard drive and will not ever be accessible to anyone else but you. We can follow a hierarchical structure as we did before when we stored the configuration information for the Mapbox access token.

Let’s create a key named Google and a sub-key under that named ApiKey with the value of the API Key:

{
  "Google": {
    "ApiKey": " AIzaSyAP1b08VzDZkhYoI9hdCEcyzE5PEjyoGC4"
  }
}

To access that value, we can use the same mechanism as before to inject an IConfiguration instance into our class constructor, and then use configuration["Google:ApiKey"] to access the value. Don’t worry about doing that now though - we will get to that later.


If you find this book helpful and want to return the favour, you can Buy me a coffee


Hey, while you're here: I publish a weekly newsletter for ASP.NET Developers called ASP.NET Weekly, which helps you stay up to date on all the latest ASP.NET related news and blog posts. Please check it out and subscribe!