[英]Azure Event Hub Authorization from Android
我正在開發一個將數據發送到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)
不是您在授權標頭中發送的SAS密鑰,而是SAS令牌。 以下是從密鑰生成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.