Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 480.6k

UnathorizedException Error?

Sep 12 2019 1:56 AM
Hi Team
 
I am writing a back end application in C# using Azure SDK to the portal, but the exception im getting is as follow;
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Azure.Devices.Client;  
  7. using Microsoft.Azure.Devices.Common;  
  8. using Microsoft.Azure.Devices.Shared;  
  9. using Microsoft.Azure.Devices;  
  10. namespace BackEndApplication  
  11. {  
  12. class Program  
  13. {  
  14. private static ServiceClient _serviceClient;  
  15. private readonly static string s_connectionString = "HostName=UniversityIOTHub.azure-devices.net;DeviceId=UniversityDeviceIOT;SharedAccessKey=U433bDviQRc4QlxyZSO+9sZ1d6fY94CJg2AMh+bF78g=";  
  16. private static async Task InvokeMethod()  
  17. {  
  18. var methodInvocation = new CloudToDeviceMethod("SetTelemetryInterval") { ResponseTimeout = TimeSpan.FromSeconds(30) };  
  19. methodInvocation.SetPayloadJson("10");  
  20. var response = await _serviceClient.InvokeDeviceMethodAsync("UniversityDeviceIOT", methodInvocation);  
  21. Console.WriteLine("Response status:{0}, payload", response.Status);  
  22. Console.WriteLine(response.GetPayloadAsJson());  
  23. }  
  24. static void Main(string[] args)  
  25. {  
  26. Console.WriteLine("IOT Hub Test-- BackEndApplication.\n");  
  27. _serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString);  
  28. InvokeMethod().GetAwaiter().GetResult();  
  29. Console.WriteLine("Press Enter to exit.");  
  30. Console.ReadLine();  
  31. }  
  32. }  
  33. }  
Microsoft.Azure.Devices.Common.Exceptions.UnauthorizedException
HResult=0x80131500
Message={"Message":"{\"errorCode\":401002,\"trackingId\":\"56456d99a904426686d7547826371c50-G:2-TimeStamp:09/12/2019 06:54:47\",\"message\":\"Unauthorized\",\"timestampUtc\":\"2019-09-12T06:54:47.6295403Z\"}","ExceptionMessage":""}
Source=Microsoft.Azure.Devices
StackTrace:
at Microsoft.Azure.Devices.HttpClientHelper.<ExecuteAsync>d__36.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.HttpClientHelper.<PostAsync>d__27`2.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at BackEndApplication.Program.<InvokeMethod>d__2.MoveNext() in C:\Users\Gcobanim\source\repos\BackEndApplication\Program.cs:line 25
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at BackEndApplication.Program.Main(String[] args) in C:\Users\Gcobanim\source\repos\BackEndApplication\Program.cs:line 36

Answers (4)