来自Android的Azure Event Hub授权 [英] Azure Event Hub Authorization from Android
问题描述
我正在开发一个将数据发送到azure事件中心的应用程序.这类似于此处的博客文章: http://sreesharp.com/send-events-from-android-app-to-microsoft-azure-event-hubs/
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/
但是,我更新了连接代码以使用OkHttp:
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();
}
}
但是,这将返回事件中心未经授权"的响应.我正在使用的sas密钥用于我使用发送和侦听权限创建的共享访问策略.它是主键.
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.
我在这里做错了什么?在这种情况下,Azure文档并没有真正帮助我,因为它专注于使用与Android不兼容的Azure Java库(即它们需要Java 1.8)
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)
推荐答案
这不是您在授权"标头中发送的SAS密钥,而是SAS令牌.以下是从密钥生成SAS令牌的5种或6种不同的语言: https://docs.microsoft.com/zh-cn/azure/service-bus-messaging/service-bus-sas-overview
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
这篇关于来自Android的Azure Event Hub授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!