当我试图在eclipse中运行我的python代码时,我收到了这个错误:
这是我的代码:
"""
Module Name: deviceManager.py
Project: IoTHubRestSample
Copyright (c) Microsoft Corporation.
Using [Device Indentities REST APIs](https://msdn.microsoft.com/en-us/library/azure/mt548489.aspx) to create a new device identity, retrieve a device identity, and list device identities.
This source is subject to the Microsoft Public License.
See http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
All other rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
"""
import base64
import hmac
import hashlib
import time
import requests
import urllib
class DeviceManager:
API_VERSION = '2016-02-03'
TOKEN_VALID_SECS = 365 * 24 * 60 * 60
TOKEN_FORMAT = 'SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s'
def __init__(self, connectionString=None):
if connectionString != None:
iotHost, keyName, keyValue = [sub[sub.index('=') + 1:] for sub in connectionString.split(";")]
self.iotHost = iotHost
self.keyName = keyName
self.keyValue = keyValue
def _buildExpiryOn(self):
return '%d' % (time.time() + self.TOKEN_VALID_SECS)
def _buildSasToken(self):
targetUri = self.iotHost.lower()
expiryTime = self._buildExpiryOn()
toSign = '%s\n%s' % (targetUri, expiryTime)
key = base64.b64decode(self.keyValue.encode('utf-8'))
signature = urllib.quote(
base64.b64encode(
hmac.HMAC(key, toSign.encode('utf-8'), hashlib.sha256).digest()
)
).replace('/', '%2F')
return self.TOKEN_FORMAT % (targetUri, signature, expiryTime, self.keyName)
def createDeviceId(self, deviceId):
sasToken = self._buildSasToken()
url = 'https://%s/devices/%s?api-version=%s' % (self.iotHost, deviceId, self.API_VERSION)
body = '{deviceId: "%s"}' % deviceId
r = requests.put(url, headers={'Content-Type': 'application/json', 'Authorization': sasToken}, data=body)
return r.text, r.status_code
def retrieveDeviceId(self, deviceId):
sasToken = self._buildSasToken()
url = 'https://%s/devices/%s?api-version=%s' % (self.iotHost, deviceId, self.API_VERSION)
r = requests.get(url, headers={'Content-Type': 'application/json', 'Authorization': sasToken})
return r.text, r.status_code
def listDeviceIds(self, top=None):
if top == None:
top = 1000
sasToken = self._buildSasToken()
url = 'https://%s/devices?top=%d&api-version=%s' % (self.iotHost, top, self.API_VERSION)
r = requests.get(url, headers={'Content-Type': 'application/json', 'Authorization': sasToken})
return r.text, r.status_code
def deleteDeviceId(self, deviceId):
sasToken = self._buildSasToken()
url = 'https://%s/devices/%s?api-version=%s' % (self.iotHost, deviceId, self.API_VERSION)
r = requests.delete(url, headers={'Content-Type': 'application/json', 'Authorization': sasToken, 'If-Match': '*' })
# If-Match Etag, but if * is used, no need to precise the Etag of the device. The Etag of the device can be seen in the header requests.text response
return r.text, r.status_code
if __name__ == '__main__':
connectionString = 'HostName=<iot-hub-name>.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=<iothubowner-policy-key>'
dm = DeviceManager(connectionString)
deviceId='iotdevice1'
print(dm.createDeviceId(deviceId))
print(dm.retrieveDeviceId(deviceId))
print(dm.listDeviceIds())
如何解决这个问题?
答案 0 :(得分:0)
您的链接代码似乎是由我编写的Azure代码示例,来自here及其在here的介绍。根据您的错误屏幕截图,我看到代码connectionString = 'HostName=<iot-hub-name>.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=<iothubowner-policy-key>'
未被更改。如果你没有,请按照官方教程Create an IoT hub using the Azure portal
创建一个IoTHub,然后复制你的IoT集线器名称&amp;密钥(如下所示)而不是<iot-hub-name>
&amp; <iothubowner-policy-key>
。