Last active
April 10, 2019 19:48
-
-
Save HowardvanRooijen/ebbac1286222d23c3ffe to your computer and use it in GitHub Desktop.
Post Data into Azure Event Hubs using Web Api Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Task<HttpResponseMessage> PostTelemetryAsync(DeviceTelemetry deviceTelemetry) | |
{ | |
// Use Event Hubs Signature Generator 0.2.0.1 to generate the token | |
// https://github.com/sandrinodimattia/RedDog/releases/tag/0.2.0.1 | |
// http://fabriccontroller.net/blog/posts/iot-with-azure-service-bus-event-hubs-authenticating-and-sending-from-any-type-of-device-net-and-js-samples/ | |
var sas = "SharedAccessSignature sr=YOUR TOKEN HERE"; | |
// Namespace info. | |
var serviceNamespace = "YOUR NAMESPACE"; | |
var hubName = "YOUR HUB NAME"; | |
var url = string.Format("{0}/publishers/{1}/messages", hubName, deviceTelemetry.DeviceId); | |
// Create client. | |
var httpClient = new HttpClient | |
{ | |
BaseAddress = new Uri(string.Format("https://{0}.servicebus.windows.net/", serviceNamespace)) | |
}; | |
var payload = JsonConvert.SerializeObject(deviceTelemetry); | |
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", sas); | |
var content = new StringContent(payload, Encoding.UTF8, "application/json"); | |
content.Headers.Add("ContentType", DeviceTelemetry.ContentType); | |
return httpClient.PostAsync(url, content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the token provided by "YOUR TOKEN HERE" provided by creating a new Sas key in the Azure portal?