如何管理 Azure EventGrid 授权方案?

问题描述

我正在尝试用 C# 发布一个事件,但收到有关授权方案的错误

我正在 Azure 中的应用服务中的 webjob 中执行代码。 这是给我带来问题的代码

Using Microsoft.Azure.EventGrid;

var eventGridClient = new EventGridClient(credentials);
client.PublishEventsAsync(topicHostname,events).Wait();

我得到的错误是这样的:

Microsoft.Rest.Azure.CloudException:请求有一个不受支持的 授权方案:承载。授权方案必须是 共享访问签名。

我可以在哪里管理?

解决方法

从错误消息来看,您使用了错误的凭据,应该是如下代码。

string topicEndpoint = "https://<topic-name>.<region>-1.eventgrid.azure.net/api/events";
string topicKey = "<topic-key>";
string topicHostname = new Uri(topicEndpoint).Host;

TopicCredentials topicCredentials = new TopicCredentials(topicKey);
EventGridClient client = new EventGridClient(topicCredentials);
client.PublishEventsAsync(topicHostname,events).Wait();

参考 - https://docs.microsoft.com/en-us/dotnet/api/overview/azure/eventgrid#publish-events