Azure Event Hub Authorization from Android

前提是你 提交于 2019-12-25 07:50:03

问题


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:


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



来源:https://stackoverflow.com/questions/42005370/azure-event-hub-authorization-from-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!