Reputation: 61
I installed Nuget package Microsoft.Azure.EventGrid 1.1.0-preview and found that it contains class EventGridClient. I assumed that I can use it to publish EventGridEvent. The ctor looks like this:
public EventGridClient(ServiceClientCredentials credentials, params DelegatingHandler[] handlers);
I could not find any Doc on how to use this class. I am wondering how I should create the ServiceClientCredentials for this ctor.
I tried to create it like this:
var credential = new AzureCredentialsFactory().FromServicePrincipal(AuthClientId, clientCert, AADMicrosoftTenant, AzureEnvironment.AzureGlobalCloud);
var client = new EventGridClient(credential);
It does not work. The request and response looks like these:
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."
}]
}
}
It does look like that EventGrid does not support Bearer token. So, how should I use the class EventGridClient?
Upvotes: 3
Views: 2754
Reputation: 301
Azure Event Grid requires either the "aeg-sas-token" or the "aeg-sas-key" authorization header in the publish request. The TopicCredentials class in the SDK can be used to specify the topic key. From the topic you created, get the topic key (e.g. using Azure Portal or by using az eventgrid topic key list -n topic-name -g resource-group-name). The following is an example of how to publish to Event Grid using this library:
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;
}
Upvotes: 2