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

Visual FoxPro
LOCAL lnSuccess
LOCAL loConfig
LOCAL loHttp
LOCAL loResponse
LOCAL loResult
LOCAL loQuery
LOCAL loForm
LOCAL loFeatures
LOCAL loFeature
LOCAL i

lnSuccess = 0

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

loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/maps-entra.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private Entra configuration."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.JsonTypeOf("tenant_id") <> 1) THEN
    ? "Missing or invalid tenant_id."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.StringOfEquals("tenant_id","",1) = 1) THEN
    ? "A nonempty tenant_id is required."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.JsonTypeOf("client_id") <> 1) THEN
    ? "Missing or invalid client_id."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.StringOfEquals("client_id","",1) = 1) THEN
    ? "A nonempty client_id is required."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.JsonTypeOf("client_secret") <> 1) THEN
    ? "Missing or invalid client_secret."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.StringOfEquals("client_secret","",1) = 1) THEN
    ? "A nonempty client_secret is required."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.JsonTypeOf("maps_client_id") <> 1) THEN
    ? "Missing or invalid maps_client_id."
    RELEASE loConfig
    CANCEL
ENDIF

IF (loConfig.StringOfEquals("maps_client_id","",1) = 1) THEN
    ? "A nonempty maps_client_id is required."
    RELEASE loConfig
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 30
loHttp.ReadTimeout = 90
loHttp.Accept = "application/json"
loResponse = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
loQuery = CreateObject('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.
lnSuccess = loHttp.SetUrlVar("tenant",loConfig.StringOf("tenant_id"))
IF (lnSuccess <> 1) THEN
    ? "Could not set tenant."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    CANCEL
ENDIF

loForm = CreateObject('Chilkat.HttpRequest')
loForm.HttpVerb = "POST"
loForm.ContentType = "application/x-www-form-urlencoded"
loForm.AddParam("grant_type","client_credentials")
loForm.AddParam("client_id",loConfig.StringOf("client_id"))
loForm.AddParam("client_secret",loConfig.StringOf("client_secret"))
loForm.AddParam("scope","https://atlas.microsoft.com/.default")
lnSuccess = loHttp.HttpReq("https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token",loForm,loResponse)
*  Http methods return false for HTTP errors as well as transport failures.
IF (loResponse.StatusCode = 0) THEN
    ? "No HTTP response. Inspect diagnostics privately; they can contain credentials."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (loResponse.StatusCode <> 200) THEN
    ? "HTTP status: " + STR(loResponse.StatusCode)
    ? "Error code: " + loResponse.GetHeaderField("x-ms-error-code")
    ? "Retry-After: " + loResponse.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.
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (lnSuccess <> 1) THEN
    ? "The HTTP response was incomplete."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

lnSuccess = loResult.Load(loResponse.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (loResult.JsonTypeOf("access_token") <> 1) THEN
    ? "Missing or invalid access_token."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (loResult.StringOfEquals("access_token","",1) = 1) THEN
    ? "A nonempty access_token is required."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (loResult.JsonTypeOf("expires_in") <> 2) THEN
    ? "Missing or invalid expires_in."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (loResult.StringOfEquals("token_type","Bearer",0) <> 1) THEN
    ? "Expected a Bearer token."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

? "Token lifetime in seconds: " + loResult.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.
loHttp.AuthToken = loResult.StringOf("access_token")
*  This is the MAPS ACCOUNT client ID, not the Entra application client_id.
loHttp.SetRequestHeader("x-ms-client-id",loConfig.StringOf("maps_client_id"))
loQuery.UpdateString("api-version","2026-01-01")
loQuery.UpdateString("query","1 Microsoft Way, Redmond, WA")
loQuery.UpdateInt("top",3)
lnSuccess = loHttp.HttpParams("GET","https://atlas.microsoft.com/geocode",loQuery,loResponse)
*  Http methods return false for HTTP errors as well as transport failures.
IF (loResponse.StatusCode = 0) THEN
    ? "No HTTP response. Inspect diagnostics privately; they can contain credentials."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (loResponse.StatusCode <> 200) THEN
    ? "HTTP status: " + STR(loResponse.StatusCode)
    ? "Error code: " + loResponse.GetHeaderField("x-ms-error-code")
    ? "Retry-After: " + loResponse.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.
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

IF (lnSuccess <> 1) THEN
    ? "The HTTP response was incomplete."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

lnSuccess = loResult.Load(loResponse.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    CANCEL
ENDIF

loFeatures = CreateObject('Chilkat.JsonArray')
loFeature = CreateObject('Chilkat.JsonObject')
IF (loResult.StringOfEquals("type","FeatureCollection",1) <> 1) THEN
    ? "Expected a GeoJSON FeatureCollection."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    RELEASE loFeatures
    RELEASE loFeature
    CANCEL
ENDIF

lnSuccess = loResult.ArrayOf2("features",loFeatures)
IF (lnSuccess <> 1) THEN
    ? "Expected a features array."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    RELEASE loFeatures
    RELEASE loFeature
    CANCEL
ENDIF

IF (loFeatures.Size = 0) THEN
    ? "No matching locations."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loResponse
    RELEASE loResult
    RELEASE loQuery
    RELEASE loForm
    RELEASE loFeatures
    RELEASE loFeature
    CANCEL
ENDIF

i = 0
DO WHILE (i < loFeatures.Size)
    lnSuccess = loFeatures.ObjectAt2(i,loFeature)
    IF (lnSuccess <> 1) THEN
        ? "Invalid feature."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loResponse
        RELEASE loResult
        RELEASE loQuery
        RELEASE loForm
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    IF (loFeature.JsonTypeOf("properties.address.formattedAddress") <> 1) THEN
        ? "Missing or invalid properties.address.formattedAddress."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loResponse
        RELEASE loResult
        RELEASE loQuery
        RELEASE loForm
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    ? "Address: " + loFeature.StringOf("properties.address.formattedAddress")
    IF (loFeature.JsonTypeOf("geometry.coordinates[0]") <> 2) THEN
        ? "Missing or invalid geometry.coordinates[0]."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loResponse
        RELEASE loResult
        RELEASE loQuery
        RELEASE loForm
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    IF (loFeature.JsonTypeOf("geometry.coordinates[1]") <> 2) THEN
        ? "Missing or invalid geometry.coordinates[1]."
        RELEASE loConfig
        RELEASE loHttp
        RELEASE loResponse
        RELEASE loResult
        RELEASE loQuery
        RELEASE loForm
        RELEASE loFeatures
        RELEASE loFeature
        CANCEL
    ENDIF

    *  GeoJSON coordinates are longitude, latitude.
    ? "Longitude: " + loFeature.StringOf("geometry.coordinates[0]")
    ? "Latitude: " + loFeature.StringOf("geometry.coordinates[1]")
    IF (loFeature.JsonTypeOf("properties.confidence") = 1) THEN
        ? "Confidence: " + loFeature.StringOf("properties.confidence")
    ENDIF

    i = i + 1
ENDDO

RELEASE loConfig
RELEASE loHttp
RELEASE loResponse
RELEASE loResult
RELEASE loQuery
RELEASE loForm
RELEASE loFeatures
RELEASE loFeature