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

Lua
local chilkat = require("chilkat")

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

local config = chilkat.JsonObject.new()
config.strict = true
local success = config:load_file("qa_data/azure/maps-entra.json")
if not success then
    print("Could not load private Entra configuration.")
    return
end

if config:json_type_of("tenant_id") ~= 1 then
    print("Missing or invalid tenant_id.")
    return
end

if config:string_of_equals("tenant_id", "", true) then
    print("A nonempty tenant_id is required.")
    return
end

if config:json_type_of("client_id") ~= 1 then
    print("Missing or invalid client_id.")
    return
end

if config:string_of_equals("client_id", "", true) then
    print("A nonempty client_id is required.")
    return
end

if config:json_type_of("client_secret") ~= 1 then
    print("Missing or invalid client_secret.")
    return
end

if config:string_of_equals("client_secret", "", true) then
    print("A nonempty client_secret is required.")
    return
end

if config:json_type_of("maps_client_id") ~= 1 then
    print("Missing or invalid maps_client_id.")
    return
end

if config:string_of_equals("maps_client_id", "", true) then
    print("A nonempty maps_client_id is required.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 90
http.accept = "application/json"
local response = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local query = chilkat.JsonObject.new()
-- 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:set_url_var("tenant", config:string_of("tenant_id"))
if not success then
    print("Could not set tenant.")
    return
end

local form = chilkat.HttpRequest.new()
form.http_verb = "POST"
form.content_type = "application/x-www-form-urlencoded"
form:add_param("grant_type", "client_credentials")
form:add_param("client_id", config:string_of("client_id"))
form:add_param("client_secret", config:string_of("client_secret"))
form:add_param("scope", "https://atlas.microsoft.com/.default")
success = http:http_req("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.status_code == 0 then
    print("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
    return
end

if response.status_code ~= 200 then
    print("HTTP status: " .. response.status_code)
    print("Error code: " .. response:get_header_field("x-ms-error-code"))
    print("Retry-After: " .. response:get_header_field("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 not success then
    print("The HTTP response was incomplete.")
    return
end

success = result:load(response.body_str)
if not success then
    print("Expected a well-formed JSON response.")
    return
end

if result:json_type_of("access_token") ~= 1 then
    print("Missing or invalid access_token.")
    return
end

if result:string_of_equals("access_token", "", true) then
    print("A nonempty access_token is required.")
    return
end

if result:json_type_of("expires_in") ~= 2 then
    print("Missing or invalid expires_in.")
    return
end

if not result:string_of_equals("token_type", "Bearer", false) then
    print("Expected a Bearer token.")
    return
end

print("Token lifetime in seconds: " .. result:string_of("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.auth_token = result:string_of("access_token")
-- This is the MAPS ACCOUNT client ID, not the Entra application client_id.
http:set_request_header("x-ms-client-id", config:string_of("maps_client_id"))
query:update_string("api-version", "2026-01-01")
query:update_string("query", "1 Microsoft Way, Redmond, WA")
query:update_int("top", 3)
success = http:http_params("GET", "https://atlas.microsoft.com/geocode", query, response)
-- Http methods return false for HTTP errors as well as transport failures.
if response.status_code == 0 then
    print("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
    return
end

if response.status_code ~= 200 then
    print("HTTP status: " .. response.status_code)
    print("Error code: " .. response:get_header_field("x-ms-error-code"))
    print("Retry-After: " .. response:get_header_field("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 not success then
    print("The HTTP response was incomplete.")
    return
end

success = result:load(response.body_str)
if not success then
    print("Expected a well-formed JSON response.")
    return
end

local features = chilkat.JsonArray.new()
local feature = chilkat.JsonObject.new()
if not result:string_of_equals("type", "FeatureCollection", true) then
    print("Expected a GeoJSON FeatureCollection.")
    return
end

success = result:array_of2("features", features)
if not success then
    print("Expected a features array.")
    return
end

if features.size == 0 then
    print("No matching locations.")
    return
end

local i = 0
while i < features.size do
    success = features:object_at2(i, feature)
    if not success then
        print("Invalid feature.")
        return
    end

    if feature:json_type_of("properties.address.formattedAddress") ~= 1 then
        print("Missing or invalid properties.address.formattedAddress.")
        return
    end

    print("Address: " .. feature:string_of("properties.address.formattedAddress"))
    if feature:json_type_of("geometry.coordinates[0]") ~= 2 then
        print("Missing or invalid geometry.coordinates[0].")
        return
    end

    if feature:json_type_of("geometry.coordinates[1]") ~= 2 then
        print("Missing or invalid geometry.coordinates[1].")
        return
    end

    -- GeoJSON coordinates are longitude, latitude.
    print("Longitude: " .. feature:string_of("geometry.coordinates[0]"))
    print("Latitude: " .. feature:string_of("geometry.coordinates[1]"))
    if feature:json_type_of("properties.confidence") == 1 then
        print("Confidence: " .. feature:string_of("properties.confidence"))
    end

    i = i + 1
end