【问题标题】:Azure Notification Hub error 401Azure 通知中心错误 401
【发布时间】:2016-07-18 09:11:54
【问题描述】:

我在设置将推送通知发送到 iOS/Android 设备的控制台应用程序时遇到了一些问题。

错误如下:

Unhandled Exception: System.AggregateException: One or more errors
occurred. ---System.UnauthorizedAccessException: The remote server
returned an error: (401)
Unauthorized..TrackingId:ea158550-8761-41f8-821d-dbcb88fcce56,TimeStamp:18/07/2016
08:58:21 ---System.Net.WebException: The remote server returned an
error: (401) Unauthorized.    at
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)   
at
Microsoft.Azure.NotificationHubs.NotificationRequestAsyncResult`1.<GetAsyncSteps>b__3(TAsyncResult
thisPtr, IAsyncResult r)    at
Microsoft.Azure.NotificationHubs.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult
result)    --- End of inner exception stack trace ---

Server stack trace:


Exception rethrown at [0]:    at
Microsoft.Azure.NotificationHubs.Common.AsyncResult.End[TAsyncResult](IAsyncResult
result)    at
Microsoft.Azure.NotificationHubs.NotificationHubManager.EndSendNotification(IAsyncResult
result)    at
System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult
iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean
requiresSynchronization)    --- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean
includeTaskCanceledExceptions)    at
System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout,
CancellationToken cancellationToken)    at
System.Threading.Tasks.Task.Wait()    at
XamarinAzurePushNotification.Server.Program.Main(String[] args) in
B:\Personal Development\Visual Studio
2015\AzurePushNotifications\XamarinAzurePushNotification.Server\Program.cs:line
26

这是我的控制台应用程序代码:

private static void Main(string[] args)
{
    var hub = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://ConnectionStringName.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=ConnectionStringKey", "HubName");

    var iOS_alert = "{\"aps\":{\"alert\":\"Hello. This is a iOS notification! Tada!\", \"sound\":\"default\"}}";
    hub.SendAppleNativeNotificationAsync(iOS_alert).Wait();

    var android_alert = "{\"message\": \"Can I interest you in a once in a lifetime push notification?!\", \"title\":\"Ding, dong!\"}";
    hub.SendGcmNativeNotificationAsync(android_alert).Wait();

    Console.WriteLine("MESSAGE SENT");
}

如果有人能帮我解决这个问题,那就太好了。

【问题讨论】:

  • 您能否验证您的 API 密钥是否为服务器密钥?见我的answer here
  • 你看到我已经在 Azure 中使用测试推送通知功能测试了我的应用程序,并且我的应用程序正在接收通知,但是当我尝试在我的控制台应用程序(服务器)中实现相同的功能时,它只是给出了那个错误.
  • 我刚刚检查了那里,似乎我已经在使用服务器密钥了。
  • 这很奇怪。您是否还检查了 official doc 中的建议操作以获取错误代码 401?
  • 我有,但它似乎对我没有多大帮助......

标签: c# azure google-cloud-messaging apple-push-notifications azure-notificationhub


【解决方案1】:

看起来你正在使用DefaultListenSharedAccessSignature;而是使用DefaultFullSharedAccessSignature

【讨论】:

    【解决方案2】:

    如果以上任何一个答案都不起作用,请检查服务器/PC 是否配置了正确的日期时间。在我的情况下,在客户端上,服务器的日期时间设置错误,我得到了错误:

    错误代码:0x17 System.AggregateException:发生一个或多个错误。 ---> System.UnauthorizedAccessException:远程服务器返回错误:(401)未授权。 ExpiredToken: .TrackingId:4c2cdbg3-e1a8-4240-b963-d6f55e8f2c76_G29,TimeStamp:8/23/2018 8:58:27 AM ---> System.Net.WebException:远程服务器返回错误:(401)未经授权。 在 System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 Microsoft.Azure.NotificationHubs.NotificationRequestAsyncResult1.<>c.<GetAsyncSteps>b__26_3(TAsyncResult thisPtr, IAsyncResult r) at Microsoft.Azure.NotificationHubs.Messaging.IteratorAsyncResult1.StepCallback(IAsyncResult 结果) --- 内部异常堆栈跟踪结束 ---

    在我发现问题之前非常沮丧。

    【讨论】:

    • 这里还是同一个问题,比如服务器数据是后面的时间。
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    相关资源
    最近更新 更多