【问题标题】:How to use REST API for sending messages to Azure Notification Hub from Java/GAE如何使用 REST API 从 Java/GAE 向 Azure 通知中心发送消息
【发布时间】:2015-06-15 20:16:40
【问题描述】:

我已经成功实现调用GAE -> Azure Mobile Services -> Azure Notification HUB。

但我想跳过移动服务步骤并直接调用通知 HUB,但我不知道如何发送授权令牌。返回的错误是:

Returned response: <Error><Code>401</Code><Detail>MissingAudience: The provided token does not 
specify the 'Audience'..TrackingId:6a9a452d-c3bf-4fed-b0b0-975210f7a13c_G14,TimeStamp:11/26/2013 12:47:40 PM</Detail></Error>

这是我的代码:

    URL url = new URL("https://myapp-ns.servicebus.windows.net/myhubbie/messages/?api-version=2013-08");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(60000);
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);        
    connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
    connection.setRequestProperty("Authorization","WRAP access_token=\"mytoken_taken_from_azure_portal=\"");
    connection.setRequestProperty("ServiceBusNotification-Tags", tag);


    byte[] notificationMessage = new byte[0];
    try 
    {
        notificationMessage = json.getBytes("UTF-8");
    } 
    catch (UnsupportedEncodingException e) 
    {           
        e.printStackTrace();
        log.warning("Error encoding toast message to UTF8! Error=" + e.getMessage());
    }

    connection.setRequestProperty("Content-Length", String.valueOf(notificationMessage.length));        
    OutputStream ostream = connection.getOutputStream();        
    ostream.write(notificationMessage);
    ostream.flush();
    ostream.close();

    int responseCode = connection.getResponseCode();

【问题讨论】:

  • 我也遇到了同样的问题,你解决了吗?
  • 不,我放弃并实施了不使用 GAE 的不同解决方案 -> Azure

标签: google-app-engine azure azure-notificationhub


【解决方案1】:

授权标头必须包含为每个单独的请求特制的令牌。您使用的数据是生成此类令牌所必须使用的密钥。

请按照http://msdn.microsoft.com/en-us/library/dn495627.aspx 上的说明为您的请求创建令牌。

最后请注意,如果您使用 Java,则可以使用此公共 repo https://github.com/fsautomata/notificationhubs-rest-java 中的代码。它包含一个用于通知中心的功能齐全的 REST 包装器。它不是微软官方的,但可以工作并实现上述规范。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多