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 AutoIt Downloads
Local $bSuccess = False
; This example requires the Chilkat API to have been previously unlocked.
; See Global.UnlockBundle for sample code.
$oConfig = ObjCreate("Chilkat.JsonObject")
$oConfig.Strict = True
$bSuccess = $oConfig.LoadFile("qa_data/azure/maps-entra.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load private Entra configuration." & @CRLF)
Exit
EndIf
If ($oConfig.JsonTypeOf("tenant_id") <> 1) Then
ConsoleWrite("Missing or invalid tenant_id." & @CRLF)
Exit
EndIf
If ($oConfig.StringOfEquals("tenant_id","",True) = True) Then
ConsoleWrite("A nonempty tenant_id is required." & @CRLF)
Exit
EndIf
If ($oConfig.JsonTypeOf("client_id") <> 1) Then
ConsoleWrite("Missing or invalid client_id." & @CRLF)
Exit
EndIf
If ($oConfig.StringOfEquals("client_id","",True) = True) Then
ConsoleWrite("A nonempty client_id is required." & @CRLF)
Exit
EndIf
If ($oConfig.JsonTypeOf("client_secret") <> 1) Then
ConsoleWrite("Missing or invalid client_secret." & @CRLF)
Exit
EndIf
If ($oConfig.StringOfEquals("client_secret","",True) = True) Then
ConsoleWrite("A nonempty client_secret is required." & @CRLF)
Exit
EndIf
If ($oConfig.JsonTypeOf("maps_client_id") <> 1) Then
ConsoleWrite("Missing or invalid maps_client_id." & @CRLF)
Exit
EndIf
If ($oConfig.StringOfEquals("maps_client_id","",True) = True) Then
ConsoleWrite("A nonempty maps_client_id is required." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 30
$oHttp.ReadTimeout = 90
$oHttp.Accept = "application/json"
$oResponse = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$oQuery = ObjCreate("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.
$bSuccess = $oHttp.SetUrlVar("tenant",$oConfig.StringOf("tenant_id"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not set tenant." & @CRLF)
Exit
EndIf
$oForm = ObjCreate("Chilkat.HttpRequest")
$oForm.HttpVerb = "POST"
$oForm.ContentType = "application/x-www-form-urlencoded"
$oForm.AddParam "grant_type","client_credentials"
$oForm.AddParam "client_id",$oConfig.StringOf("client_id")
$oForm.AddParam "client_secret",$oConfig.StringOf("client_secret")
$oForm.AddParam "scope","https://atlas.microsoft.com/.default"
$bSuccess = $oHttp.HttpReq("https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token",$oForm,$oResponse)
; Http methods return false for HTTP errors as well as transport failures.
If ($oResponse.StatusCode = 0) Then
ConsoleWrite("No HTTP response. Inspect diagnostics privately; they can contain credentials." & @CRLF)
Exit
EndIf
If ($oResponse.StatusCode <> 200) Then
ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
ConsoleWrite("Error code: " & $oResponse.GetHeaderField("x-ms-error-code") & @CRLF)
ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
; 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
EndIf
If ($bSuccess <> True) Then
ConsoleWrite("The HTTP response was incomplete." & @CRLF)
Exit
EndIf
$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
Exit
EndIf
If ($oResult.JsonTypeOf("access_token") <> 1) Then
ConsoleWrite("Missing or invalid access_token." & @CRLF)
Exit
EndIf
If ($oResult.StringOfEquals("access_token","",True) = True) Then
ConsoleWrite("A nonempty access_token is required." & @CRLF)
Exit
EndIf
If ($oResult.JsonTypeOf("expires_in") <> 2) Then
ConsoleWrite("Missing or invalid expires_in." & @CRLF)
Exit
EndIf
If ($oResult.StringOfEquals("token_type","Bearer",False) <> True) Then
ConsoleWrite("Expected a Bearer token." & @CRLF)
Exit
EndIf
ConsoleWrite("Token lifetime in seconds: " & $oResult.StringOf("expires_in") & @CRLF)
; 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.
$oHttp.AuthToken = $oResult.StringOf("access_token")
; This is the MAPS ACCOUNT client ID, not the Entra application client_id.
$oHttp.SetRequestHeader "x-ms-client-id",$oConfig.StringOf("maps_client_id")
$oQuery.UpdateString("api-version","2026-01-01")
$oQuery.UpdateString("query","1 Microsoft Way, Redmond, WA")
$oQuery.UpdateInt("top",3)
$bSuccess = $oHttp.HttpParams("GET","https://atlas.microsoft.com/geocode",$oQuery,$oResponse)
; Http methods return false for HTTP errors as well as transport failures.
If ($oResponse.StatusCode = 0) Then
ConsoleWrite("No HTTP response. Inspect diagnostics privately; they can contain credentials." & @CRLF)
Exit
EndIf
If ($oResponse.StatusCode <> 200) Then
ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
ConsoleWrite("Error code: " & $oResponse.GetHeaderField("x-ms-error-code") & @CRLF)
ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
; 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
EndIf
If ($bSuccess <> True) Then
ConsoleWrite("The HTTP response was incomplete." & @CRLF)
Exit
EndIf
$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
Exit
EndIf
$oFeatures = ObjCreate("Chilkat.JsonArray")
$oFeature = ObjCreate("Chilkat.JsonObject")
If ($oResult.StringOfEquals("type","FeatureCollection",True) <> True) Then
ConsoleWrite("Expected a GeoJSON FeatureCollection." & @CRLF)
Exit
EndIf
$bSuccess = $oResult.ArrayOf2("features",$oFeatures)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a features array." & @CRLF)
Exit
EndIf
If ($oFeatures.Size = 0) Then
ConsoleWrite("No matching locations." & @CRLF)
Exit
EndIf
Local $i = 0
While ($i < $oFeatures.Size)
$bSuccess = $oFeatures.ObjectAt2($i,$oFeature)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid feature." & @CRLF)
Exit
EndIf
If ($oFeature.JsonTypeOf("properties.address.formattedAddress") <> 1) Then
ConsoleWrite("Missing or invalid properties.address.formattedAddress." & @CRLF)
Exit
EndIf
ConsoleWrite("Address: " & $oFeature.StringOf("properties.address.formattedAddress") & @CRLF)
If ($oFeature.JsonTypeOf("geometry.coordinates[0]") <> 2) Then
ConsoleWrite("Missing or invalid geometry.coordinates[0]." & @CRLF)
Exit
EndIf
If ($oFeature.JsonTypeOf("geometry.coordinates[1]") <> 2) Then
ConsoleWrite("Missing or invalid geometry.coordinates[1]." & @CRLF)
Exit
EndIf
; GeoJSON coordinates are longitude, latitude.
ConsoleWrite("Longitude: " & $oFeature.StringOf("geometry.coordinates[0]") & @CRLF)
ConsoleWrite("Latitude: " & $oFeature.StringOf("geometry.coordinates[1]") & @CRLF)
If ($oFeature.JsonTypeOf("properties.confidence") = 1) Then
ConsoleWrite("Confidence: " & $oFeature.StringOf("properties.confidence") & @CRLF)
EndIf
$i = $i + 1
Wend