简体   繁体   English

来自Android的Azure Event Hub授权

[英]Azure Event Hub Authorization from Android

I'm working on an application that sends data to an azure event hub. 我正在开发一个将数据发送到azure事件中心的应用程序。 This is similar to the blog post here: http://sreesharp.com/send-events-from-android-app-to-microsoft-azure-event-hubs/ 这类似于此处的博客文章: http : //sreesharp.com/send-events-from-android-app-to-microsoft-azure-event-hubs/

However, I updated the connection code to use OkHttp: 但是,我更新了连接代码以使用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. 我使用的sas密钥用于我使用发送和监听权限创建的共享访问策略。 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 (ie they require Java 1.8) 在这种情况下,Azure文档并没有真正帮助我,因为它专注于使用与Android不兼容的Azure Java库(即它们需要Java 1.8)

It's not the SAS Key you send in the Authorization header, it's the SAS Token. 不是您在授权标头中发送的SAS密钥,而是SAS令牌。 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 以下是从密钥生成SAS令牌的5种或6种不同的语言: https : //docs.microsoft.com/zh-cn/azure/service-bus-messaging/service-bus-sas-overview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM