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 SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global.UnlockBundle for sample code.
DECLARE @config int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @config, 'Strict', 1
EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'qa_data/azure/maps-entra.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private Entra configuration.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'tenant_id'
IF @iTmp0 <> 1
BEGIN
PRINT 'Missing or invalid tenant_id.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp0 OUT, 'tenant_id', '', 1
IF @iTmp0 = 1
BEGIN
PRINT 'A nonempty tenant_id is required.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
IF @iTmp0 <> 1
BEGIN
PRINT 'Missing or invalid client_id.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp0 OUT, 'client_id', '', 1
IF @iTmp0 = 1
BEGIN
PRINT 'A nonempty client_id is required.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'client_secret'
IF @iTmp0 <> 1
BEGIN
PRINT 'Missing or invalid client_secret.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp0 OUT, 'client_secret', '', 1
IF @iTmp0 = 1
BEGIN
PRINT 'A nonempty client_secret is required.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'maps_client_id'
IF @iTmp0 <> 1
BEGIN
PRINT 'Missing or invalid maps_client_id.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp0 OUT, 'maps_client_id', '', 1
IF @iTmp0 = 1
BEGIN
PRINT 'A nonempty maps_client_id is required.'
EXEC @hr = sp_OADestroy @config
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 30
EXEC sp_OASetProperty @http, 'ReadTimeout', 90
EXEC sp_OASetProperty @http, 'Accept', 'application/json'
DECLARE @response int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
DECLARE @query int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @query OUT
-- 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.
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'tenant_id'
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'tenant', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set tenant.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
RETURN
END
DECLARE @form int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @form OUT
EXEC sp_OASetProperty @form, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @form, 'ContentType', 'application/x-www-form-urlencoded'
EXEC sp_OAMethod @form, 'AddParam', NULL, 'grant_type', 'client_credentials'
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @form, 'AddParam', NULL, 'client_id', @sTmp0
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_secret'
EXEC sp_OAMethod @form, 'AddParam', NULL, 'client_secret', @sTmp0
EXEC sp_OAMethod @form, 'AddParam', NULL, 'scope', 'https://atlas.microsoft.com/.default'
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, 'https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token', @form, @response
-- Http methods return false for HTTP errors as well as transport failures.
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No HTTP response. Inspect diagnostics privately; they can contain credentials.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'x-ms-error-code'
PRINT 'Error code: ' + @sTmp0
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
-- Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
-- Keep response bodies and detailed diagnostics out of public logs.
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP response was incomplete.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
IF @iTmp0 <> 1
BEGIN
PRINT 'Missing or invalid access_token.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'access_token', '', 1
IF @iTmp0 = 1
BEGIN
PRINT 'A nonempty access_token is required.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'expires_in'
IF @iTmp0 <> 2
BEGIN
PRINT 'Missing or invalid expires_in.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'token_type', 'Bearer', 0
IF @iTmp0 <> 1
BEGIN
PRINT 'Expected a Bearer token.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'expires_in'
PRINT 'Token lifetime in seconds: ' + @sTmp0
-- 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.
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
-- This is the MAPS ACCOUNT client ID, not the Entra application client_id.
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'maps_client_id'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-ms-client-id', @sTmp0
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'api-version', '2026-01-01'
EXEC sp_OAMethod @query, 'UpdateString', @success OUT, 'query', '1 Microsoft Way, Redmond, WA'
EXEC sp_OAMethod @query, 'UpdateInt', @success OUT, 'top', 3
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://atlas.microsoft.com/geocode', @query, @response
-- Http methods return false for HTTP errors as well as transport failures.
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No HTTP response. Inspect diagnostics privately; they can contain credentials.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'x-ms-error-code'
PRINT 'Error code: ' + @sTmp0
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
-- Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
-- Keep response bodies and detailed diagnostics out of public logs.
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP response was incomplete.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
RETURN
END
DECLARE @features int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @features OUT
DECLARE @feature int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @feature OUT
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp0 OUT, 'type', 'FeatureCollection', 1
IF @iTmp0 <> 1
BEGIN
PRINT 'Expected a GeoJSON FeatureCollection.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAMethod @result, 'ArrayOf2', @success OUT, 'features', @features
IF @success <> 1
BEGIN
PRINT 'Expected a features array.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAGetProperty @features, 'Size', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No matching locations.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @features, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OAMethod @features, 'ObjectAt2', @success OUT, @i, @feature
IF @success <> 1
BEGIN
PRINT 'Invalid feature.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp0 OUT, 'properties.address.formattedAddress'
IF @iTmp0 <> 1
BEGIN
PRINT 'Missing or invalid properties.address.formattedAddress.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'properties.address.formattedAddress'
PRINT 'Address: ' + @sTmp0
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp0 OUT, 'geometry.coordinates[0]'
IF @iTmp0 <> 2
BEGIN
PRINT 'Missing or invalid geometry.coordinates[0].'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp0 OUT, 'geometry.coordinates[1]'
IF @iTmp0 <> 2
BEGIN
PRINT 'Missing or invalid geometry.coordinates[1].'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
RETURN
END
-- GeoJSON coordinates are longitude, latitude.
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'geometry.coordinates[0]'
PRINT 'Longitude: ' + @sTmp0
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'geometry.coordinates[1]'
PRINT 'Latitude: ' + @sTmp0
EXEC sp_OAMethod @feature, 'JsonTypeOf', @iTmp0 OUT, 'properties.confidence'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @feature, 'StringOf', @sTmp0 OUT, 'properties.confidence'
PRINT 'Confidence: ' + @sTmp0
END
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @query
EXEC @hr = sp_OADestroy @form
EXEC @hr = sp_OADestroy @features
EXEC @hr = sp_OADestroy @feature
END
GO