Azure Maps: Authenticate with Microsoft Entra ID
See more Azure Maps Examples
Obtains an application-only token and uses it to geocode an address. Configure tenant_id, application client_id, secret value in client_secret, and the Azure Maps account's maps_client_id in private qa_data/azure/maps-entra.json. All four are nonempty strings. The two client IDs identify different resources.
Register the application in the account's Entra tenant and assign its service principal an appropriate Maps data role at the Maps account scope. Azure Maps Search and Render Data Reader is suitable for the geocoding request shown. Token acquisition alone does not grant Maps access. Allow time for role changes to propagate. Other operations, especially batch requests, need the corresponding data actions.
This confidential-client pattern is for a protected service or internal application that can keep a secret. It does not require a webhook or web server. For public clients, use a suitable delegated flow or backend; on supported Azure hosts consider managed identity. Do not log the token, secret, or response body. Protect the configuration file. Cache tokens with an expiry margin; obtain another using the same grant when needed, because no refresh token is issued.
See client credentials protocol for the form-encoded token request.
Chilkat Ruby Downloads
require 'chilkat'
success = false
# This example requires the Chilkat API to have been previously unlocked.
# See Global.UnlockBundle for sample code.
config = Chilkat::CkJsonObject.new()
config.put_Strict(true)
success = config.LoadFile("qa_data/azure/maps-entra.json")
if (success != true)
print "Could not load private Entra configuration." + "\n";
exit
end
if (config.JsonTypeOf("tenant_id") != 1)
print "Missing or invalid tenant_id." + "\n";
exit
end
if (config.StringOfEquals("tenant_id","",true) == true)
print "A nonempty tenant_id is required." + "\n";
exit
end
if (config.JsonTypeOf("client_id") != 1)
print "Missing or invalid client_id." + "\n";
exit
end
if (config.StringOfEquals("client_id","",true) == true)
print "A nonempty client_id is required." + "\n";
exit
end
if (config.JsonTypeOf("client_secret") != 1)
print "Missing or invalid client_secret." + "\n";
exit
end
if (config.StringOfEquals("client_secret","",true) == true)
print "A nonempty client_secret is required." + "\n";
exit
end
if (config.JsonTypeOf("maps_client_id") != 1)
print "Missing or invalid maps_client_id." + "\n";
exit
end
if (config.StringOfEquals("maps_client_id","",true) == true)
print "A nonempty maps_client_id is required." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_FollowRedirects(false)
http.put_ConnectTimeout(30)
http.put_ReadTimeout(90)
http.put_Accept("application/json")
response = Chilkat::CkHttpResponse.new()
result = Chilkat::CkJsonObject.new()
result.put_Strict(true)
query = Chilkat::CkJsonObject.new()
# This Http object is used only for the token request until it completes.
# The tenant is a directory ID or verified tenant domain, not common/consumers.
success = http.SetUrlVar("tenant",config.stringOf("tenant_id"))
if (success != true)
print "Could not set tenant." + "\n";
exit
end
form = Chilkat::CkHttpRequest.new()
form.put_HttpVerb("POST")
form.put_ContentType("application/x-www-form-urlencoded")
form.AddParam("grant_type","client_credentials")
form.AddParam("client_id",config.stringOf("client_id"))
form.AddParam("client_secret",config.stringOf("client_secret"))
form.AddParam("scope","https://atlas.microsoft.com/.default")
success = http.HttpReq("https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token",form,response)
# Http methods return false for HTTP errors as well as transport failures.
if (response.get_StatusCode() == 0)
print "No HTTP response. Inspect diagnostics privately; they can contain credentials." + "\n";
exit
end
if (response.get_StatusCode() != 200)
print "HTTP status: " + response.get_StatusCode().to_s() + "\n";
print "Error code: " + response.getHeaderField("x-ms-error-code") + "\n";
print "Retry-After: " + response.getHeaderField("Retry-After") + "\n";
# Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
# Keep response bodies and detailed diagnostics out of public logs.
exit
end
if (success != true)
print "The HTTP response was incomplete." + "\n";
exit
end
success = result.Load(response.bodyStr())
if (success != true)
print "Expected a well-formed JSON response." + "\n";
exit
end
if (result.JsonTypeOf("access_token") != 1)
print "Missing or invalid access_token." + "\n";
exit
end
if (result.StringOfEquals("access_token","",true) == true)
print "A nonempty access_token is required." + "\n";
exit
end
if (result.JsonTypeOf("expires_in") != 2)
print "Missing or invalid expires_in." + "\n";
exit
end
if (result.StringOfEquals("token_type","Bearer",false) != true)
print "Expected a Bearer token." + "\n";
exit
end
print "Token lifetime in seconds: " + result.stringOf("expires_in") + "\n";
# Cache the token privately and obtain a new one before its lifetime expires.
# Client credentials do not yield a refresh token: repeat the token request.
http.put_AuthToken(result.stringOf("access_token"))
# This is the MAPS ACCOUNT client ID, not the Entra application client_id.
http.SetRequestHeader("x-ms-client-id",config.stringOf("maps_client_id"))
query.UpdateString("api-version","2026-01-01")
query.UpdateString("query","1 Microsoft Way, Redmond, WA")
query.UpdateInt("top",3)
success = http.HttpParams("GET","https://atlas.microsoft.com/geocode",query,response)
# Http methods return false for HTTP errors as well as transport failures.
if (response.get_StatusCode() == 0)
print "No HTTP response. Inspect diagnostics privately; they can contain credentials." + "\n";
exit
end
if (response.get_StatusCode() != 200)
print "HTTP status: " + response.get_StatusCode().to_s() + "\n";
print "Error code: " + response.getHeaderField("x-ms-error-code") + "\n";
print "Retry-After: " + response.getHeaderField("Retry-After") + "\n";
# Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
# Keep response bodies and detailed diagnostics out of public logs.
exit
end
if (success != true)
print "The HTTP response was incomplete." + "\n";
exit
end
success = result.Load(response.bodyStr())
if (success != true)
print "Expected a well-formed JSON response." + "\n";
exit
end
features = Chilkat::CkJsonArray.new()
feature = Chilkat::CkJsonObject.new()
if (result.StringOfEquals("type","FeatureCollection",true) != true)
print "Expected a GeoJSON FeatureCollection." + "\n";
exit
end
success = result.ArrayOf2("features",features)
if (success != true)
print "Expected a features array." + "\n";
exit
end
if (features.get_Size() == 0)
print "No matching locations." + "\n";
exit
end
i = 0
while (i < features.get_Size())
success = features.ObjectAt2(i,feature)
if (success != true)
print "Invalid feature." + "\n";
exit
end
if (feature.JsonTypeOf("properties.address.formattedAddress") != 1)
print "Missing or invalid properties.address.formattedAddress." + "\n";
exit
end
print "Address: " + feature.stringOf("properties.address.formattedAddress") + "\n";
if (feature.JsonTypeOf("geometry.coordinates[0]") != 2)
print "Missing or invalid geometry.coordinates[0]." + "\n";
exit
end
if (feature.JsonTypeOf("geometry.coordinates[1]") != 2)
print "Missing or invalid geometry.coordinates[1]." + "\n";
exit
end
# GeoJSON coordinates are longitude, latitude.
print "Longitude: " + feature.stringOf("geometry.coordinates[0]") + "\n";
print "Latitude: " + feature.stringOf("geometry.coordinates[1]") + "\n";
if (feature.JsonTypeOf("properties.confidence") == 1)
print "Confidence: " + feature.stringOf("properties.confidence") + "\n";
end
i = i + 1
end