3

我已成功实现调用 GAE -> Azure 移动服务 -> Azure Notification 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();
4

1 回答 1

1

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

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

最后一点,如果你使用的是 Java,你可以使用这个公共仓库https://github.com/fsautomata/notificationhubs-rest-java中的代码。它包含一个用于通知中心的功能齐全的 REST 包装器。它不是 Microsoft 官方的,但可以工作并实现上述规范。

于 2014-01-17T17:51:59.293 回答