Azure Event Hub Authorization from Android

2019-09-19 08:19发布

I‘m working on an application that sends data to an azure event hub. This is similar to the blog post here:http://sreesharp.com/send-events-from-android-app-to-microsoft-azure-event-hubs/

However, I updated the connection code to use 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();
    }
}

However this returns a response from the event hub "Unauthorized". The sas key I am using is for a shared access policy that I created with send and listen permissions. It is the primary key.

What am I doing wrong here? The Azure documentation doesnt really help me in this case because it focused on using the Azure Java libraries that are not Android compatible (i.e. they require Java 1.8)

1条回答
再贱就再见
2楼-- · 2019-09-19 08:52

It's not the SAS Key you send in the Authorization header, it's the SAS Token. Here are like 5 or six different languages for generating the SAS Token from the key: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-sas-overview

查看更多
登录 后发表回答