问题
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…..
回答1:
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.
回答2:
There is an open source project that provides a .NET client for the Azure Maps REST services. There is a NuGet package too. You can find it here: https://github.com/perfahlen/AzureMapsRestServices The Azure Maps plans on also providing an official .NET client for the rest services later this year.
来源:https://stackoverflow.com/questions/55367075/how-could-i-call-azure-maps-api-in-c-sharp-with-authorisation-and-client-id