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 Xojo Plugin Downloads
Dim success As Boolean
success = False
// This example requires the Chilkat API to have been previously unlocked.
// See Global.UnlockBundle for sample code.
Dim config As New Chilkat.JsonObject
config.Strict = True
success = config.LoadFile("qa_data/azure/maps-entra.json")
If (success <> True) Then
System.DebugLog("Could not load private Entra configuration.")
Return
End If
If (config.JsonTypeOf("tenant_id") <> 1) Then
System.DebugLog("Missing or invalid tenant_id.")
Return
End If
If (config.StringOfEquals("tenant_id","",True) = True) Then
System.DebugLog("A nonempty tenant_id is required.")
Return
End If
If (config.JsonTypeOf("client_id") <> 1) Then
System.DebugLog("Missing or invalid client_id.")
Return
End If
If (config.StringOfEquals("client_id","",True) = True) Then
System.DebugLog("A nonempty client_id is required.")
Return
End If
If (config.JsonTypeOf("client_secret") <> 1) Then
System.DebugLog("Missing or invalid client_secret.")
Return
End If
If (config.StringOfEquals("client_secret","",True) = True) Then
System.DebugLog("A nonempty client_secret is required.")
Return
End If
If (config.JsonTypeOf("maps_client_id") <> 1) Then
System.DebugLog("Missing or invalid maps_client_id.")
Return
End If
If (config.StringOfEquals("maps_client_id","",True) = True) Then
System.DebugLog("A nonempty maps_client_id is required.")
Return
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 30
http.ReadTimeout = 90
http.Accept = "application/json"
Dim response As New Chilkat.HttpResponse
Dim result As New Chilkat.JsonObject
result.Strict = True
Dim query As New Chilkat.JsonObject
// 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
System.DebugLog("Could not set tenant.")
Return
End If
Dim form As New Chilkat.HttpRequest
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
System.DebugLog("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
Return
End If
If (response.StatusCode <> 200) Then
System.DebugLog("HTTP status: " + Str(response.StatusCode))
System.DebugLog("Error code: " + response.GetHeaderField("x-ms-error-code"))
System.DebugLog("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
System.DebugLog("The HTTP response was incomplete.")
Return
End If
success = result.Load(response.BodyStr)
If (success <> True) Then
System.DebugLog("Expected a well-formed JSON response.")
Return
End If
If (result.JsonTypeOf("access_token") <> 1) Then
System.DebugLog("Missing or invalid access_token.")
Return
End If
If (result.StringOfEquals("access_token","",True) = True) Then
System.DebugLog("A nonempty access_token is required.")
Return
End If
If (result.JsonTypeOf("expires_in") <> 2) Then
System.DebugLog("Missing or invalid expires_in.")
Return
End If
If (result.StringOfEquals("token_type","Bearer",False) <> True) Then
System.DebugLog("Expected a Bearer token.")
Return
End If
System.DebugLog("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")
success = query.UpdateString("api-version","2026-01-01")
success = query.UpdateString("query","1 Microsoft Way, Redmond, WA")
success = 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
System.DebugLog("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
Return
End If
If (response.StatusCode <> 200) Then
System.DebugLog("HTTP status: " + Str(response.StatusCode))
System.DebugLog("Error code: " + response.GetHeaderField("x-ms-error-code"))
System.DebugLog("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
System.DebugLog("The HTTP response was incomplete.")
Return
End If
success = result.Load(response.BodyStr)
If (success <> True) Then
System.DebugLog("Expected a well-formed JSON response.")
Return
End If
Dim features As New Chilkat.JsonArray
Dim feature As New Chilkat.JsonObject
If (result.StringOfEquals("type","FeatureCollection",True) <> True) Then
System.DebugLog("Expected a GeoJSON FeatureCollection.")
Return
End If
success = result.ArrayOf2("features",features)
If (success <> True) Then
System.DebugLog("Expected a features array.")
Return
End If
If (features.Size = 0) Then
System.DebugLog("No matching locations.")
Return
End If
Dim i As Int32
i = 0
While (i < features.Size)
success = features.ObjectAt2(i,feature)
If (success <> True) Then
System.DebugLog("Invalid feature.")
Return
End If
If (feature.JsonTypeOf("properties.address.formattedAddress") <> 1) Then
System.DebugLog("Missing or invalid properties.address.formattedAddress.")
Return
End If
System.DebugLog("Address: " + feature.StringOf("properties.address.formattedAddress"))
If (feature.JsonTypeOf("geometry.coordinates[0]") <> 2) Then
System.DebugLog("Missing or invalid geometry.coordinates[0].")
Return
End If
If (feature.JsonTypeOf("geometry.coordinates[1]") <> 2) Then
System.DebugLog("Missing or invalid geometry.coordinates[1].")
Return
End If
// GeoJSON coordinates are longitude, latitude.
System.DebugLog("Longitude: " + feature.StringOf("geometry.coordinates[0]"))
System.DebugLog("Latitude: " + feature.StringOf("geometry.coordinates[1]"))
If (feature.JsonTypeOf("properties.confidence") = 1) Then
System.DebugLog("Confidence: " + feature.StringOf("properties.confidence"))
End If
i = i + 1
Wend