Skip to content

Instantly share code, notes, and snippets.

@HowardvanRooijen
Last active April 10, 2019 19:48
Show Gist options
  • Save HowardvanRooijen/ebbac1286222d23c3ffe to your computer and use it in GitHub Desktop.
Save HowardvanRooijen/ebbac1286222d23c3ffe to your computer and use it in GitHub Desktop.
Post Data into Azure Event Hubs using Web Api Client
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);
}
@timmyreilly
Copy link

Is the token provided by "YOUR TOKEN HERE" provided by creating a new Sas key in the Azure portal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment