【问题标题】:Azure Event Hub Authorization from Android来自 Android 的 Azure 事件中心授权
【发布时间】:2017-06-19 17:03:15
【问题描述】:

我正在开发一个将数据发送到 Azure 事件中心的应用程序。这和这里的博文类似:http://sreesharp.com/send-events-from-android-app-to-microsoft-azure-event-hubs/

不过,我更新了连接代码以使用 OkHttp:

public void sendMessageOkHttp(String dataPacket, String connectionString, String sasKey){

    // Instantiate the OkHttp Client
    OkHttpClient client = new OkHttpClient();

    // Create the body of the message to be send
    RequestBody formBody = new FormBody.Builder()
            .add("message", dataPacket)
            .build();

    // Now create the request and post it
    Request request = new Request.Builder()
            .header("Authorization", sasKey)
            .url(connectionString)
            .post(formBody)
            .build();
    Log.i(TAG,"about to send message");
    // Now try to send the message
    try {
        Log.i(TAG,"sending message....");
        Response response = client.newCall(request).execute();
        Log.i(TAG,"message sent");
        Log.i("Azure Response",String.valueOf(response.message()));

        // Do something with the response.
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是,这会从事件中心“未经授权”返回响应。我使用的 sas 密钥用于我创建的具有发送和侦听权限的共享访问策略。它是主键。

我在这里做错了什么?在这种情况下,Azure 文档并没有真正帮助我,因为它专注于使用与 Android 不兼容的 Azure Java 库(即它们需要 Java 1.8)

【问题讨论】:

    标签: java android okhttp azure-eventhub


    【解决方案1】:

    这不是您在授权标头中发送的 SAS 密钥,而是 SAS 令牌。这里有 5 或 6 种不同的语言用于从密钥生成 SAS 令牌:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-sas-overview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-18
      • 2022-08-24
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2016-12-14
      • 2022-08-18
      相关资源
      最近更新 更多