如何使用身份验证和客户端 ID 在 C# 中调用 Azure Maps API?

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

我正在尝试使用 A​​zure Maps API 来使用点的坐标搜索点周围的 POI,但我不知道如何调用 API 通过添加 A​​uthorizationclient-id.

这是我在 Microsoft 文档网站上尝试 API 时得到的请求预览。

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…..

您可以使用 RestSharp。授权和 client-id 添加为 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;
}

不要忘记先安装 RestSharp NuGet 包。

有一个开源项目为 Azure Maps REST 服务提供 .NET 客户端。还有一个 NuGet 包。您可以在这里找到它:https://github.com/perfahlen/AzureMapsRestServices Azure Maps 计划在今年晚些时候为其余服务提供官方 .NET 客户端。