Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoConfig
    Handle hoHttp
    Variant vResponse
    Handle hoResponse
    Handle hoResult
    Variant vQuery
    Handle hoQuery
    Variant vForm
    Handle hoForm
    Variant vFeatures
    Handle hoFeatures
    Variant vFeature
    Handle hoFeature
    Integer i
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
    If (Not(IsComObjectCreated(hoConfig))) Begin
        Send CreateComObject of hoConfig
    End
    Set ComStrict Of hoConfig To True
    Get ComLoadFile Of hoConfig "qa_data/azure/maps-entra.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private Entra configuration."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoConfig "tenant_id" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid tenant_id."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoConfig "tenant_id" "" True To bTemp1
    If (bTemp1 = True) Begin
        Showln "A nonempty tenant_id is required."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoConfig "client_id" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid client_id."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoConfig "client_id" "" True To bTemp1
    If (bTemp1 = True) Begin
        Showln "A nonempty client_id is required."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoConfig "client_secret" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid client_secret."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoConfig "client_secret" "" True To bTemp1
    If (bTemp1 = True) Begin
        Showln "A nonempty client_secret is required."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoConfig "maps_client_id" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid maps_client_id."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoConfig "maps_client_id" "" True To bTemp1
    If (bTemp1 = True) Begin
        Showln "A nonempty maps_client_id is required."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComFollowRedirects Of hoHttp To False
    Set ComConnectTimeout Of hoHttp To 30
    Set ComReadTimeout Of hoHttp To 90
    Set ComAccept Of hoHttp To "application/json"
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
    If (Not(IsComObjectCreated(hoResponse))) Begin
        Send CreateComObject of hoResponse
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Get Create (RefClass(cComChilkatJsonObject)) To hoQuery
    If (Not(IsComObjectCreated(hoQuery))) Begin
        Send CreateComObject of hoQuery
    End
    //  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.
    Get ComStringOf Of hoConfig "tenant_id" To sTemp1
    Get ComSetUrlVar Of hoHttp "tenant" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set tenant."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoForm
    If (Not(IsComObjectCreated(hoForm))) Begin
        Send CreateComObject of hoForm
    End
    Set ComHttpVerb Of hoForm To "POST"
    Set ComContentType Of hoForm To "application/x-www-form-urlencoded"
    Send ComAddParam To hoForm "grant_type" "client_credentials"
    Get ComStringOf Of hoConfig "client_id" To sTemp1
    Send ComAddParam To hoForm "client_id" sTemp1
    Get ComStringOf Of hoConfig "client_secret" To sTemp1
    Send ComAddParam To hoForm "client_secret" sTemp1
    Send ComAddParam To hoForm "scope" "https://atlas.microsoft.com/.default"
    Get pvComObject of hoForm to vForm
    Get pvComObject of hoResponse to vResponse
    Get ComHttpReq Of hoHttp "https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token" vForm vResponse To iSuccess
    //  Http methods return false for HTTP errors as well as transport failures.
    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No HTTP response. Inspect diagnostics privately; they can contain credentials."
        Procedure_Return
    End

    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResponse To iTemp1
        Showln "HTTP status: " iTemp1
        Get ComGetHeaderField Of hoResponse "x-ms-error-code" To sTemp1
        Showln "Error code: " sTemp1
        Get ComGetHeaderField Of hoResponse "Retry-After" To sTemp1
        Showln "Retry-After: " sTemp1
        //  Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
        //  Keep response bodies and detailed diagnostics out of public logs.
        Procedure_Return
    End

    If (iSuccess <> True) Begin
        Showln "The HTTP response was incomplete."
        Procedure_Return
    End

    Get ComBodyStr Of hoResponse To sTemp1
    Get ComLoad Of hoResult sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a well-formed JSON response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoResult "access_token" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Missing or invalid access_token."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoResult "access_token" "" True To bTemp1
    If (bTemp1 = True) Begin
        Showln "A nonempty access_token is required."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoResult "expires_in" To iTemp1
    If (iTemp1 <> 2) Begin
        Showln "Missing or invalid expires_in."
        Procedure_Return
    End

    Get ComStringOfEquals Of hoResult "token_type" "Bearer" False To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Expected a Bearer token."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "expires_in" To sTemp1
    Showln "Token lifetime in seconds: " sTemp1
    //  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.
    Get ComStringOf Of hoResult "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    //  This is the MAPS ACCOUNT client ID, not the Entra application client_id.
    Get ComStringOf Of hoConfig "maps_client_id" To sTemp1
    Send ComSetRequestHeader To hoHttp "x-ms-client-id" sTemp1
    Get ComUpdateString Of hoQuery "api-version" "2026-01-01" To iSuccess
    Get ComUpdateString Of hoQuery "query" "1 Microsoft Way, Redmond, WA" To iSuccess
    Get ComUpdateInt Of hoQuery "top" 3 To iSuccess
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResponse to vResponse
    Get ComHttpParams Of hoHttp "GET" "https://atlas.microsoft.com/geocode" vQuery vResponse To iSuccess
    //  Http methods return false for HTTP errors as well as transport failures.
    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No HTTP response. Inspect diagnostics privately; they can contain credentials."
        Procedure_Return
    End

    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusCode Of hoResponse To iTemp1
        Showln "HTTP status: " iTemp1
        Get ComGetHeaderField Of hoResponse "x-ms-error-code" To sTemp1
        Showln "Error code: " sTemp1
        Get ComGetHeaderField Of hoResponse "Retry-After" To sTemp1
        Showln "Retry-After: " sTemp1
        //  Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
        //  Keep response bodies and detailed diagnostics out of public logs.
        Procedure_Return
    End

    If (iSuccess <> True) Begin
        Showln "The HTTP response was incomplete."
        Procedure_Return
    End

    Get ComBodyStr Of hoResponse To sTemp1
    Get ComLoad Of hoResult sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a well-formed JSON response."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonArray)) To hoFeatures
    If (Not(IsComObjectCreated(hoFeatures))) Begin
        Send CreateComObject of hoFeatures
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoFeature
    If (Not(IsComObjectCreated(hoFeature))) Begin
        Send CreateComObject of hoFeature
    End
    Get ComStringOfEquals Of hoResult "type" "FeatureCollection" True To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Expected a GeoJSON FeatureCollection."
        Procedure_Return
    End

    Get pvComObject of hoFeatures to vFeatures
    Get ComArrayOf2 Of hoResult "features" vFeatures To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a features array."
        Procedure_Return
    End

    Get ComSize Of hoFeatures To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No matching locations."
        Procedure_Return
    End

    Move 0 To i
    While (i < (ComSize(hoFeatures)))
        Get pvComObject of hoFeature to vFeature
        Get ComObjectAt2 Of hoFeatures i vFeature To iSuccess
        If (iSuccess <> True) Begin
            Showln "Invalid feature."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoFeature "properties.address.formattedAddress" To iTemp1
        If (iTemp1 <> 1) Begin
            Showln "Missing or invalid properties.address.formattedAddress."
            Procedure_Return
        End

        Get ComStringOf Of hoFeature "properties.address.formattedAddress" To sTemp1
        Showln "Address: " sTemp1
        Get ComJsonTypeOf Of hoFeature "geometry.coordinates[0]" To iTemp1
        If (iTemp1 <> 2) Begin
            Showln "Missing or invalid geometry.coordinates[0]."
            Procedure_Return
        End

        Get ComJsonTypeOf Of hoFeature "geometry.coordinates[1]" To iTemp1
        If (iTemp1 <> 2) Begin
            Showln "Missing or invalid geometry.coordinates[1]."
            Procedure_Return
        End

        //  GeoJSON coordinates are longitude, latitude.
        Get ComStringOf Of hoFeature "geometry.coordinates[0]" To sTemp1
        Showln "Longitude: " sTemp1
        Get ComStringOf Of hoFeature "geometry.coordinates[1]" To sTemp1
        Showln "Latitude: " sTemp1
        Get ComJsonTypeOf Of hoFeature "properties.confidence" To iTemp1
        If (iTemp1 = 1) Begin
            Get ComStringOf Of hoFeature "properties.confidence" To sTemp1
            Showln "Confidence: " sTemp1
        End

        Move (i + 1) To i
    Loop



End_Procedure