Search code examples
c#azureapiazure-maps

How could I call Azure Maps API in C# with authorisation and client id?


I am trying to use Azure Maps API to search POIs around a point using its coordinates, and I do not know how to call the API by adding the Authorization and client-id.

This is the request preview I get when I try the API on the Microsoft documentation website.

GET https://atlas.microsoft.com/search/poi/json?api-version=1.0&query=university&lat=10.8232&lon=2.98234&limit=1

Authorization: Bearer eyJ0eXAiOiJKV1……

X-ms-client-id: SUXrtewLVItIG3X5…..

Solution

  • You could use RestSharp. The authorization and client-id are added as header:

    using RestSharp;
    
    string url = $"https://atlas.microsoft.com/search/poi/json?api-version=1.0&query=university&lat=10.8232&lon=2.98234&limit=1";
    
    var client = new RestClient(url);
    var request = new RestRequest(Method.GET);
    
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Authorization", “Bearer eyJ0eXAiOiJKV1……”);
    request.AddHeader("X-ms-client-id", “SUXrtewLVItIG3X5…..”);
    
    IRestResponse response = client.Execute(request);
    
    if (response.IsSuccessful)
    {
        string content = response.Content;
    }
    

    Don't forget to start by installing the RestSharp NuGet package.