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 Delphi ActiveX Downloads
var
success: Integer;
config: TChilkatJsonObject;
http: TChilkatHttp;
response: TChilkatHttpResponse;
result: TChilkatJsonObject;
query: TChilkatJsonObject;
form: TChilkatHttpRequest;
features: TChilkatJsonArray;
feature: TChilkatJsonObject;
i: Integer;
begin
success := 0;
// This example requires the Chilkat API to have been previously unlocked.
// See Global.UnlockBundle for sample code.
config := TChilkatJsonObject.Create(Self);
config.Strict := 1;
success := config.LoadFile('qa_data/azure/maps-entra.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load private Entra configuration.');
Exit;
end;
if (config.JsonTypeOf('tenant_id') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid tenant_id.');
Exit;
end;
if (config.StringOfEquals('tenant_id','',1) = 1) then
begin
Memo1.Lines.Add('A nonempty tenant_id is required.');
Exit;
end;
if (config.JsonTypeOf('client_id') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid client_id.');
Exit;
end;
if (config.StringOfEquals('client_id','',1) = 1) then
begin
Memo1.Lines.Add('A nonempty client_id is required.');
Exit;
end;
if (config.JsonTypeOf('client_secret') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid client_secret.');
Exit;
end;
if (config.StringOfEquals('client_secret','',1) = 1) then
begin
Memo1.Lines.Add('A nonempty client_secret is required.');
Exit;
end;
if (config.JsonTypeOf('maps_client_id') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid maps_client_id.');
Exit;
end;
if (config.StringOfEquals('maps_client_id','',1) = 1) then
begin
Memo1.Lines.Add('A nonempty maps_client_id is required.');
Exit;
end;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
http.ConnectTimeout := 30;
http.ReadTimeout := 90;
http.Accept := 'application/json';
response := TChilkatHttpResponse.Create(Self);
result := TChilkatJsonObject.Create(Self);
result.Strict := 1;
query := TChilkatJsonObject.Create(Self);
// 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.SetUrlVar('tenant',config.StringOf('tenant_id'));
if (success <> 1) then
begin
Memo1.Lines.Add('Could not set tenant.');
Exit;
end;
form := TChilkatHttpRequest.Create(Self);
form.HttpVerb := 'POST';
form.ContentType := 'application/x-www-form-urlencoded';
form.AddParam('grant_type','client_credentials');
form.AddParam('client_id',config.StringOf('client_id'));
form.AddParam('client_secret',config.StringOf('client_secret'));
form.AddParam('scope','https://atlas.microsoft.com/.default');
success := http.HttpReq('https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token',form.ControlInterface,response.ControlInterface);
// Http methods return false for HTTP errors as well as transport failures.
if (response.StatusCode = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect diagnostics privately; they can contain credentials.');
Exit;
end;
if (response.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(response.StatusCode));
Memo1.Lines.Add('Error code: ' + response.GetHeaderField('x-ms-error-code'));
Memo1.Lines.Add('Retry-After: ' + response.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.
Exit;
end;
if (success <> 1) then
begin
Memo1.Lines.Add('The HTTP response was incomplete.');
Exit;
end;
success := result.Load(response.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
if (result.JsonTypeOf('access_token') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid access_token.');
Exit;
end;
if (result.StringOfEquals('access_token','',1) = 1) then
begin
Memo1.Lines.Add('A nonempty access_token is required.');
Exit;
end;
if (result.JsonTypeOf('expires_in') <> 2) then
begin
Memo1.Lines.Add('Missing or invalid expires_in.');
Exit;
end;
if (result.StringOfEquals('token_type','Bearer',0) <> 1) then
begin
Memo1.Lines.Add('Expected a Bearer token.');
Exit;
end;
Memo1.Lines.Add('Token lifetime in seconds: ' + result.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.
http.AuthToken := result.StringOf('access_token');
// This is the MAPS ACCOUNT client ID, not the Entra application client_id.
http.SetRequestHeader('x-ms-client-id',config.StringOf('maps_client_id'));
query.UpdateString('api-version','2026-01-01');
query.UpdateString('query','1 Microsoft Way, Redmond, WA');
query.UpdateInt('top',3);
success := http.HttpParams('GET','https://atlas.microsoft.com/geocode',query.ControlInterface,response.ControlInterface);
// Http methods return false for HTTP errors as well as transport failures.
if (response.StatusCode = 0) then
begin
Memo1.Lines.Add('No HTTP response. Inspect diagnostics privately; they can contain credentials.');
Exit;
end;
if (response.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(response.StatusCode));
Memo1.Lines.Add('Error code: ' + response.GetHeaderField('x-ms-error-code'));
Memo1.Lines.Add('Retry-After: ' + response.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.
Exit;
end;
if (success <> 1) then
begin
Memo1.Lines.Add('The HTTP response was incomplete.');
Exit;
end;
success := result.Load(response.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a well-formed JSON response.');
Exit;
end;
features := TChilkatJsonArray.Create(Self);
feature := TChilkatJsonObject.Create(Self);
if (result.StringOfEquals('type','FeatureCollection',1) <> 1) then
begin
Memo1.Lines.Add('Expected a GeoJSON FeatureCollection.');
Exit;
end;
success := result.ArrayOf2('features',features.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a features array.');
Exit;
end;
if (features.Size = 0) then
begin
Memo1.Lines.Add('No matching locations.');
Exit;
end;
i := 0;
while (i < features.Size) do
begin
success := features.ObjectAt2(i,feature.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Invalid feature.');
Exit;
end;
if (feature.JsonTypeOf('properties.address.formattedAddress') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid properties.address.formattedAddress.');
Exit;
end;
Memo1.Lines.Add('Address: ' + feature.StringOf('properties.address.formattedAddress'));
if (feature.JsonTypeOf('geometry.coordinates[0]') <> 2) then
begin
Memo1.Lines.Add('Missing or invalid geometry.coordinates[0].');
Exit;
end;
if (feature.JsonTypeOf('geometry.coordinates[1]') <> 2) then
begin
Memo1.Lines.Add('Missing or invalid geometry.coordinates[1].');
Exit;
end;
// GeoJSON coordinates are longitude, latitude.
Memo1.Lines.Add('Longitude: ' + feature.StringOf('geometry.coordinates[0]'));
Memo1.Lines.Add('Latitude: ' + feature.StringOf('geometry.coordinates[1]'));
if (feature.JsonTypeOf('properties.confidence') = 1) then
begin
Memo1.Lines.Add('Confidence: ' + feature.StringOf('properties.confidence'));
end;
i := i + 1;
end;