如何在Microsoft.Azure.EventGrid中使用EventGridClient,版本= 1.1.0.0

时间:2018-04-30 22:47:40

标签: azure-eventgrid

我安装了Nuget包Microsoft.Azure.EventGrid 1.1.0-preview,发现它包含类EventGridClient。我假设我可以用它来发布EventGridEvent。 ctor看起来像这样:

public EventGridClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers);

我找不到任何关于如何使用此课程的文档。我想知道如何为这个ctor创建ServiceClientCredentials。

我试图像这样创建它:

var credential = new AzureCredentialsFactory().FromServicePrincipal(AuthClientId, clientCert, AADMicrosoftTenant, AzureEnvironment.AzureGlobalCloud);
var client = new EventGridClient(credential);

它不起作用。请求和响应如下所示:

POST /api/events?api-version=2018-01-01 HTTP/1.1
x-ms-client-request-id: b24f14c7-b665-4ec2-9d07-309d912d10d2
accept-language: en-US
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImlCakwxUmNxemhpeTRmcHhJeGRacW9oTTJZayIsImtpZCI6ImlCakwxUmNxemhpeTRmcHhJeGRacW9oTTJZayJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuY29yZS53aW5kb3dzLm5ldC8iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC83MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDcvIiwiaWF0IjoxNTI1MTI1OTg4LCJuYmYiOjE1MjUxMjU5ODgsImV4cCI6MTUyNTEyOTg4OCwiYWlvIjoiWTJkZ1lNamEvR0s3VnMvbU5ybURMYkdydW44YkFRQT0iLCJhcHBpZCI6IjExOWVhMjg2LTA0YzMtNDNjOC05Njg0LTc0ZTMxZGYxYmM0YSIsImFwcGlkYWNyIjoiMiIsImVfZXhwIjoyNjI4MDAsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI0Ny8iLCJvaWQiOiI2NGNkYzhlOS01NDE2LTRlMWYtODlhOC1jMTA3NzNkM2ZlN2YiLCJzdWIiOiI2NGNkYzhlOS01NDE2LTRlMWYtODlhOC1jMTA3NzNkM2ZlN2YiLCJ0aWQiOiI3MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDciLCJ1dGkiOiIxc2ZHMzlkSVlrT2MwWmRJS0cwQ0FBIiwidmVyIjoiMS4wIn0.lfkWbS1wluYtTddW9QKbrg4CB6fGVwtSIM9Ej9GIi1rl81B2kY9I748T8rzNrWRVIgONKZnEGSkb3SHc6wnparB-CleUB0eoRFDzWd6p7JtLqCpgRPX5kV0n40j4UFenlG4-mjzpc2_LMMegCQcybgI-E8L_WOkTqNBC9F1XUeIL2BiB1576TMNhNYtjKOAxZrtF3MF4aeme1LbtYPUSaBAf4fkZylLoqSb1vRYh_mlmsW7mfzQfHVV9XwLG3XHinbG6S9vHiImk991KS35aN_0BKigFi-oqBn04QiIgpjy2vs6sxYhxh4Lk4tmr6Sy1onnG_vVR97ivf4VJzIRJbg
User-Agent: FxVersion/4.7.2633.0 OSName/Windows10Enterprise OSVersion/6.3.16299 Microsoft.Azure.EventGrid.EventGridClient/1.1.0.0
Content-Type: application/json; charset=utf-8
Host: <myeventgrid>.eventgrid.azure.net
Request-Context: appId=cid-v1:e7156f9b-72af-44ad-a9fc-164b3e3bf231
x-ms-request-root-id: ab41c5de-48d04a59fac6e8d5
x-ms-request-id: |ab41c5de-48d04a59fac6e8d5.3.
Request-Id: |ab41c5de-48d04a59fac6e8d5.3.
Content-Length: 1217
Expect: 100-continue
Connection: close

[
  {
    "id": "ad49ac59-681e-4917-9d42-4051d385e3f3",
    "subject": "Myapp/myobject",
    "data": {
      "Name": "Legal Name",
      "UniqueId": "c21b5c89-dfbd-44e0-9d79-95af276d58b9",
    },
    "eventType": "MyEventType",
    "eventTime": "2018-04-30T22:11:24.145413Z",
    "dataVersion": "1.0"
  }
]

HTTP/1.1 401 Request must contain one of the following authorization headers: aeg-sas-token, aeg-sas-key. Report '9a0d43b9-81d4-47d3-bd2a-f5645e774e94:4/30/2018 10:11:29 PM (UTC)' to our forums for assistance or raise a support ticket.
Content-Length: 621
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Strict-Transport-Security: max-age=31536000; includeSubDomains
x-ms-request-id: 9a0d43b9-81d4-47d3-bd2a-f5645e774e94
Date: Mon, 30 Apr 2018 22:11:29 GMT
Connection: close

{
    "error": {
        "code": "Unauthorized",
        "message": "Request must contain one of the following authorization headers: aeg-sas-token, aeg-sas-key. Report '9a0d43b9-81d4-47d3-bd2a-f5645e774e94:4/30/2018 10:11:29 PM (UTC)' to our forums for assistance or raise a support ticket.",
        "details": [{
            "code": "Unauthorized",
            "message": "Request must contain one of the following authorization headers: aeg-sas-token, aeg-sas-key. Report '9a0d43b9-81d4-47d3-bd2a-f5645e774e94:4/30/2018 10:11:29 PM (UTC)' to our forums for assistance or raise a support ticket."
    }]
  }
}

看起来EventGrid似乎不支持Bearer令牌。那么,我应该如何使用EventGridClient类?

1 个答案:

答案 0 :(得分:1)

Azure Event Grid需要&#34; aeg-sas-token&#34;或者&#34; aeg-sas-key&#34;发布请求中的授权标头。 SDK中的TopicCredentials类可用于指定主题密钥。从您创建的主题中,获取主题密钥(例如,使用Azure门户或使用az eventgrid主题密钥列表-n topic-name -g resource-group-name)。以下是如何使用此库发布到Event Grid的示例:

string topicEndpoint = "https://<topic-name>.<region-name>-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);
await client.PublishEventsAsync(topicHostname, GetEventsList());

static IList<EventGridEvent> GetEventsList()
{
    List<EventGridEvent> eventsList = new List<EventGridEvent>();

    for (int i = 0; i < 1; i++)
    {
        eventsList.Add(new EventGridEvent()
        {
            Id = Guid.NewGuid().ToString(),
            Data = new EventSpecificData()
            {
                Field1 = "Value1",
                Field2 = "Value2",
                Field3 = "Value3"
            },
            EventTime = DateTime.Now,
            EventType = "Contoso.Items.ItemReceived",
            Subject = "Door1",
            DataVersion = "1.0"
        });
    }
    return eventsList;
}