Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.6.0+

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.

Microsoft API reference

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  This example requires the Chilkat API to have been previously unlocked.
#  See Global.UnlockBundle for sample code.

set config [new_CkJsonObject]

CkJsonObject_put_Strict $config 1
set success [CkJsonObject_LoadFile $config "qa_data/azure/maps-entra.json"]
if {$success != 1} then {
    puts "Could not load private Entra configuration."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_JsonTypeOf $config "tenant_id"] != 1} then {
    puts "Missing or invalid tenant_id."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_StringOfEquals $config "tenant_id" "" 1] == 1} then {
    puts "A nonempty tenant_id is required."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_JsonTypeOf $config "client_id"] != 1} then {
    puts "Missing or invalid client_id."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_StringOfEquals $config "client_id" "" 1] == 1} then {
    puts "A nonempty client_id is required."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_JsonTypeOf $config "client_secret"] != 1} then {
    puts "Missing or invalid client_secret."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_StringOfEquals $config "client_secret" "" 1] == 1} then {
    puts "A nonempty client_secret is required."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_JsonTypeOf $config "maps_client_id"] != 1} then {
    puts "Missing or invalid maps_client_id."
    delete_CkJsonObject $config
    exit
}

if {[CkJsonObject_StringOfEquals $config "maps_client_id" "" 1] == 1} then {
    puts "A nonempty maps_client_id is required."
    delete_CkJsonObject $config
    exit
}

set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 30
CkHttp_put_ReadTimeout $http 90
CkHttp_put_Accept $http "application/json"
set response [new_CkHttpResponse]

set result [new_CkJsonObject]

CkJsonObject_put_Strict $result 1
set query [new_CkJsonObject]

#  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.
set success [CkHttp_SetUrlVar $http "tenant" [CkJsonObject_stringOf $config "tenant_id"]]
if {$success != 1} then {
    puts "Could not set tenant."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    exit
}

set form [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $form "POST"
CkHttpRequest_put_ContentType $form "application/x-www-form-urlencoded"
CkHttpRequest_AddParam $form "grant_type" "client_credentials"
CkHttpRequest_AddParam $form "client_id" [CkJsonObject_stringOf $config "client_id"]
CkHttpRequest_AddParam $form "client_secret" [CkJsonObject_stringOf $config "client_secret"]
CkHttpRequest_AddParam $form "scope" "https://atlas.microsoft.com/.default"
set success [CkHttp_HttpReq $http "https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token" $form $response]
#  Http methods return false for HTTP errors as well as transport failures.
if {[CkHttpResponse_get_StatusCode $response] == 0} then {
    puts "No HTTP response. Inspect diagnostics privately; they can contain credentials."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {[CkHttpResponse_get_StatusCode $response] != 200} then {
    puts "HTTP status: [CkHttpResponse_get_StatusCode $response]"
    puts "Error code: [CkHttpResponse_getHeaderField $response x-ms-error-code]"
    puts "Retry-After: [CkHttpResponse_getHeaderField $response 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.
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {$success != 1} then {
    puts "The HTTP response was incomplete."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $response]]
if {$success != 1} then {
    puts "Expected a well-formed JSON response."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {[CkJsonObject_JsonTypeOf $result "access_token"] != 1} then {
    puts "Missing or invalid access_token."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {[CkJsonObject_StringOfEquals $result "access_token" "" 1] == 1} then {
    puts "A nonempty access_token is required."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {[CkJsonObject_JsonTypeOf $result "expires_in"] != 2} then {
    puts "Missing or invalid expires_in."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {[CkJsonObject_StringOfEquals $result "token_type" "Bearer" 0] != 1} then {
    puts "Expected a Bearer token."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

puts "Token lifetime in seconds: [CkJsonObject_stringOf $result 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.
CkHttp_put_AuthToken $http [CkJsonObject_stringOf $result "access_token"]
#  This is the MAPS ACCOUNT client ID, not the Entra application client_id.
CkHttp_SetRequestHeader $http "x-ms-client-id" [CkJsonObject_stringOf $config "maps_client_id"]
CkJsonObject_UpdateString $query "api-version" "2026-01-01"
CkJsonObject_UpdateString $query "query" "1 Microsoft Way, Redmond, WA"
CkJsonObject_UpdateInt $query "top" 3
set success [CkHttp_HttpParams $http "GET" "https://atlas.microsoft.com/geocode" $query $response]
#  Http methods return false for HTTP errors as well as transport failures.
if {[CkHttpResponse_get_StatusCode $response] == 0} then {
    puts "No HTTP response. Inspect diagnostics privately; they can contain credentials."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {[CkHttpResponse_get_StatusCode $response] != 200} then {
    puts "HTTP status: [CkHttpResponse_get_StatusCode $response]"
    puts "Error code: [CkHttpResponse_getHeaderField $response x-ms-error-code]"
    puts "Retry-After: [CkHttpResponse_getHeaderField $response 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.
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

if {$success != 1} then {
    puts "The HTTP response was incomplete."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $response]]
if {$success != 1} then {
    puts "Expected a well-formed JSON response."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    exit
}

set features [new_CkJsonArray]

set feature [new_CkJsonObject]

if {[CkJsonObject_StringOfEquals $result "type" "FeatureCollection" 1] != 1} then {
    puts "Expected a GeoJSON FeatureCollection."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    delete_CkJsonArray $features
    delete_CkJsonObject $feature
    exit
}

set success [CkJsonObject_ArrayOf2 $result "features" $features]
if {$success != 1} then {
    puts "Expected a features array."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    delete_CkJsonArray $features
    delete_CkJsonObject $feature
    exit
}

if {[CkJsonArray_get_Size $features] == 0} then {
    puts "No matching locations."
    delete_CkJsonObject $config
    delete_CkHttp $http
    delete_CkHttpResponse $response
    delete_CkJsonObject $result
    delete_CkJsonObject $query
    delete_CkHttpRequest $form
    delete_CkJsonArray $features
    delete_CkJsonObject $feature
    exit
}

set i 0
while {$i < [CkJsonArray_get_Size $features]} {
    set success [CkJsonArray_ObjectAt2 $features $i $feature]
    if {$success != 1} then {
        puts "Invalid feature."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonObject $query
        delete_CkHttpRequest $form
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    if {[CkJsonObject_JsonTypeOf $feature "properties.address.formattedAddress"] != 1} then {
        puts "Missing or invalid properties.address.formattedAddress."
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonObject $query
        delete_CkHttpRequest $form
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    puts "Address: [CkJsonObject_stringOf $feature properties.address.formattedAddress]"
    if {[CkJsonObject_JsonTypeOf $feature "geometry.coordinates[0]"] != 2} then {
        puts "{Missing or invalid geometry.coordinates[0].}"
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonObject $query
        delete_CkHttpRequest $form
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    if {[CkJsonObject_JsonTypeOf $feature "geometry.coordinates[1]"] != 2} then {
        puts "{Missing or invalid geometry.coordinates[1].}"
        delete_CkJsonObject $config
        delete_CkHttp $http
        delete_CkHttpResponse $response
        delete_CkJsonObject $result
        delete_CkJsonObject $query
        delete_CkHttpRequest $form
        delete_CkJsonArray $features
        delete_CkJsonObject $feature
        exit
    }

    #  GeoJSON coordinates are longitude, latitude.
    puts "Longitude: [CkJsonObject_stringOf $feature {geometry.coordinates[0]}]"
    puts "Latitude: [CkJsonObject_stringOf $feature {geometry.coordinates[1]}]"
    if {[CkJsonObject_JsonTypeOf $feature "properties.confidence"] == 1} then {
        puts "Confidence: [CkJsonObject_stringOf $feature properties.confidence]"
    }

    set i [expr $i + 1]
}

delete_CkJsonObject $config
delete_CkHttp $http
delete_CkHttpResponse $response
delete_CkJsonObject $result
delete_CkJsonObject $query
delete_CkHttpRequest $form
delete_CkJsonArray $features
delete_CkJsonObject $feature