我嘗試對MS Azure IoT Hub的設備雙胞胎調用一些HTTP GET調用。通過REST的Azure IoT Hub導致未授權
正如你可以看到在未經授權的IoTHubUnauthorizedAccess錯誤代碼GET通話效果。
我使用Azure Device Explorer爲授權標頭生成令牌,如下圖所示。
任何這方面有任何想法?我已經在這裏搜索,唯一的topic沒有幫助我。
我嘗試對MS Azure IoT Hub的設備雙胞胎調用一些HTTP GET調用。通過REST的Azure IoT Hub導致未授權
正如你可以看到在未經授權的IoTHubUnauthorizedAccess錯誤代碼GET通話效果。
我使用Azure Device Explorer爲授權標頭生成令牌,如下圖所示。
任何這方面有任何想法?我已經在這裏搜索,唯一的topic沒有幫助我。
看起來您正嘗試使用REST API從「設備」(如您使用從設備ID /密鑰對生成的SAS令牌)訪問Device Twins。 這是無法完成的,因爲通過MQTT而不是通過HTTP(請參閱下面關於IoT Hub端點和雙胞胎的文檔的鏈接)完成與「設備」雙向設備的交互。如果您想要使用設備的Twins設備,我建議您查看Azure IOT設備SDK。如果您想了解更多關於使用MQTT的信息,可以閱讀this。
但是,如果您想從後端透視圖使用設備Twins(如使用爲設備設置所需屬性的後端應用程序,讀取設備報告的屬性並使用標籤),則需要使用SAS Token生成其中一個IoT Hub共享訪問策略名稱/密鑰(不是設備憑據)。嘗試使用相同的設備資源管理器工具生成SAS令牌,但是在此「配置」選項卡上。
設備上雙胞胎一些文檔,可以幫助這一切更加清晰:
可以使用郵差預請求腳本沙箱生成SAS令牌。這是我寫的一篇博客文章,詳細介紹了生成SAS令牌需要做的所有事情。 http://blog.jongallant.com/2017/02/azure-iot-hub-device-twin-rest-apis-postman-newman/
這裏有一個郵差集合會確切告訴你如何執行的API:https://www.getpostman.com/collections/84a38008cd07accf565e
這裏有更多的郵差/ Azure的相關帖子(也是我自己): http://blog.jongallant.com/tags/postman/
下面的代碼,我在預請求腳本沙箱使用:
var resourceName = postman.getEnvironmentVariable("resourceName");
var resourceKey = postman.getEnvironmentVariable("resourceKey");
var tokenExpirationPeriod = postman.getEnvironmentVariable("tokenExpirationPeriod");
var policyKeyName = postman.getEnvironmentVariable("policyKeyName");
postman.clearEnvironmentVariable("deviceTwinSasToken"); // clear out token on first run.
// See this doc for details: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-security
var resourceUri = encodeURIComponent(resourceName + '.azure-devices.net'); // The resource uri
var expiry = Math.ceil((Date.now()/1000) + tokenExpirationPeriod * 60); // Expire the token 60 minutes from now
var uriExpiry = resourceUri + '\n' + expiry; // this is the string format to gen signature from
var decodedKey = CryptoJS.enc.Base64.parse(resourceKey); // The SHA256 key is the Base64 decoded version of the IoT Hub key
var signature = CryptoJS.HmacSHA256(uriExpiry, decodedKey); // The signature generated from the decodedKey
var encodedUri = encodeURIComponent(CryptoJS.enc.Base64.stringify(signature)); // The url encoded version of the Base64 signature
// Construct authorization string (shared access signature)
var deviceTwinSasToken = "SharedAccessSignature sr=" + resourceUri + "&sig=" + encodedUri + "&se=" + expiry;
// Add token if one is present
if (policyKeyName) {
deviceTwinSasToken += "&skn="+ policyKeyName;
}
// Put in variable to be used in other requests.
postman.setEnvironmentVariable("deviceTwinSasToken", deviceTwinSasToken);
console.log("Shared Access Signature:" + postman.getEnvironmentVariable("deviceTwinSasToken"));
從your picture,您使用指定的設備SAS令牌。當您需要使用IoT Hub SAS令牌時,它可以授予access control and permissions。您可以使用設備瀏覽器這樣得到它:
併成功發佈GET請求後,你將獲得設備雙胞胎的信息,郵遞員,它看起來像這樣:
這確實是我考慮的「錯誤」觀點。我從後端執行,但使用設備中的SAS。謝謝你的提示。 –