Key-Based Authentication
Get an overview of a class of web APIs that require key-based authentication.
Another class of APIs requires the client to authenticate himself when accessing the service. Authentication can be done via several techniques. In this paragraph, we’ll use the simplest one: access key. An access key is a generated string containing characters and digits and associated to a user.
Of course, authentication-based APIs often also have rate limits.
There is no universal standard regarding access keys. Each service is free to use its own custom format. The client must provide its access key when accessing the API, generally by adding it at the end of the API URL. A prerequisite for using any key-based web API is to generate an access key for this particular service.
Let’s put this into practice for obtaining about the current weather in your area. To do so, you could simply look outside the window, but it’s way cooler to use the Weather Underground web service instead. This service has a key-based API for retrieving the weather in any place. To obtain it, you’ll have to sign up as a user (it’s free) and generate a new API key by registering your application.
Once you’ve done this, you can access the weather data from here. Replace ACCESS_KEY
, COUNTRY
and TOWN
with your own settings, and you should obtain the weather in your surroundings. The necessary first step is to check out and understand the API data format. The result of an API call looks like this when getting the weather for Bordeaux, France.
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "Bordeaux, France",
"city": "Bordeaux",
"state": "33",
...
},
"observation_location": {
"full": "Bordeaux, ",
"city": "Bordeaux",
"state": "",
"country": "FR",
...
},
"estimated": {},
"station_id": "LFBD",
"observation_time": "Last Updated on June 28, 9:30 PM CEST",
...
}
}
Get hands-on with 1400+ tech skills courses.