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 B4X Downloads
Dim success As Boolean = False
' This example requires the Chilkat API to have been previously unlocked.
' See Global.UnlockBundle for sample code.
Dim config As ChilkatJsonObject
config.Initialize
config.Strict = True
success = config.LoadFile("qa_data/azure/maps-entra.json")
If success <> True Then
Log("Could not load private Entra configuration.")
Return
End If
If config.JsonTypeOf("tenant_id") <> 1 Then
Log("Missing or invalid tenant_id.")
Return
End If
If config.StringOfEquals("tenant_id", "", True) = True Then
Log("A nonempty tenant_id is required.")
Return
End If
If config.JsonTypeOf("client_id") <> 1 Then
Log("Missing or invalid client_id.")
Return
End If
If config.StringOfEquals("client_id", "", True) = True Then
Log("A nonempty client_id is required.")
Return
End If
If config.JsonTypeOf("client_secret") <> 1 Then
Log("Missing or invalid client_secret.")
Return
End If
If config.StringOfEquals("client_secret", "", True) = True Then
Log("A nonempty client_secret is required.")
Return
End If
If config.JsonTypeOf("maps_client_id") <> 1 Then
Log("Missing or invalid maps_client_id.")
Return
End If
If config.StringOfEquals("maps_client_id", "", True) = True Then
Log("A nonempty maps_client_id is required.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 30
http.ReadTimeout = 90
http.Accept = "application/json"
Dim response As ChilkatHttpResponse
response.Initialize
Dim result As ChilkatJsonObject
result.Initialize
result.Strict = True
Dim query As ChilkatJsonObject
query.Initialize
' 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 Then
Log("Could not set tenant.")
Return
End If
Dim form As ChilkatHttpRequest
form.Initialize
form.HttpVerb = "POST"
form.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.StatusCode = 0 Then
Log("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
Return
End If
If response.StatusCode <> 200 Then
Log("HTTP status: " & response.StatusCode)
Log("Error code: " & response.GetHeaderField("x-ms-error-code"))
Log("Retry-After: " & response.GetHeaderField("Retry-After"))
' Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
' Keep response bodies and detailed diagnostics out of public logs.
Return
End If
If success <> True Then
Log("The HTTP response was incomplete.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Expected a well-formed JSON response.")
Return
End If
If result.JsonTypeOf("access_token") <> 1 Then
Log("Missing or invalid access_token.")
Return
End If
If result.StringOfEquals("access_token", "", True) = True Then
Log("A nonempty access_token is required.")
Return
End If
If result.JsonTypeOf("expires_in") <> 2 Then
Log("Missing or invalid expires_in.")
Return
End If
If result.StringOfEquals("token_type", "Bearer", False) <> True Then
Log("Expected a Bearer token.")
Return
End If
Log("Token lifetime in seconds: " & result.StringOf("expires_in"))
' 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.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.StatusCode = 0 Then
Log("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
Return
End If
If response.StatusCode <> 200 Then
Log("HTTP status: " & response.StatusCode)
Log("Error code: " & response.GetHeaderField("x-ms-error-code"))
Log("Retry-After: " & response.GetHeaderField("Retry-After"))
' Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
' Keep response bodies and detailed diagnostics out of public logs.
Return
End If
If success <> True Then
Log("The HTTP response was incomplete.")
Return
End If
success = result.Load(response.BodyStr)
If success <> True Then
Log("Expected a well-formed JSON response.")
Return
End If
Dim features As ChilkatJsonArray
features.Initialize
Dim feature As ChilkatJsonObject
feature.Initialize
If result.StringOfEquals("type", "FeatureCollection", True) <> True Then
Log("Expected a GeoJSON FeatureCollection.")
Return
End If
success = result.ArrayOf2("features", features)
If success <> True Then
Log("Expected a features array.")
Return
End If
If features.Size = 0 Then
Log("No matching locations.")
Return
End If
Dim i As Int = 0
Do While (i < features.Size)
success = features.ObjectAt2(i, feature)
If success <> True Then
Log("Invalid feature.")
Return
End If
If feature.JsonTypeOf("properties.address.formattedAddress") <> 1 Then
Log("Missing or invalid properties.address.formattedAddress.")
Return
End If
Log("Address: " & feature.StringOf("properties.address.formattedAddress"))
If feature.JsonTypeOf("geometry.coordinates[0]") <> 2 Then
Log("Missing or invalid geometry.coordinates[0].")
Return
End If
If feature.JsonTypeOf("geometry.coordinates[1]") <> 2 Then
Log("Missing or invalid geometry.coordinates[1].")
Return
End If
' GeoJSON coordinates are longitude, latitude.
Log("Longitude: " & feature.StringOf("geometry.coordinates[0]"))
Log("Latitude: " & feature.StringOf("geometry.coordinates[1]"))
If feature.JsonTypeOf("properties.confidence") = 1 Then
Log("Confidence: " & feature.StringOf("properties.confidence"))
End If
i = i + 1
Loop