从飞利浦 hue 远程 API 获取令牌时出错



我正在关注远程 api 文档 (https://developers.meethue.com/develop/hue-api/remote-authentication/),但在通过摘要式身份验证请求令牌时出现服务器错误。

我正在用python构建请求(也尝试了php和bash):

s1=clientid+":"+realm+":"+secret
s2="POST:/oauth2/token"
hash1 = hashlib.md5(s1).hexdigest()
hash2 = hashlib.md5(s2).hexdigest()
hash  = hashlib.md5(hash1+":"+nonce+":"+hash2).hexdigest()
authheader = 'Digest username='+ clientid +', realm='+ realm +', nonce='+ nonce +', uri=/oauth2/token, response='+ hash
head = {'Authorization': authheader}
req = requests.Request('POST',url,headers=head)

发送给 meethue.com 的请求和响应如下:

('nonce=', '35cdbe20fb0456c6802d7537*********')
REQUEST
{
'_body_position': None,
'_cookies': <RequestsCookieJar[]>,
'body': None,
'headers': {'Content-Length': '0', 'Content-Type': 'application/json', 'Authorization': 'Digest username=CGopN1NNypOEaGvjQq*************, [realm=oauth2_client@api.meethue.com](mailto:realm=oauth2_client@api.meethue.com), nonce=35cdbe20fb0456c6802d753**************, uri=/oauth2/token, response=72e926c2392a23492793******************'},
'hooks': { 'response': []},
'method': 'POST',
'url': 'https://api.meethue.com/oauth2/token?code=M8DkG*******&grant_type=authorization_code'
}
RESPONSE
{
'_content': '{"fault":{"faultstring":"invalid_request","detail":{"errorcode":"invalid_request"}}}',
'_content_consumed': True,
'_next': None,
'connection': <requests.adapters.HTTPAdapter object at 0x7f4a50ea5390>,
'cookies': <RequestsCookieJar[]>,
'elapsed': datetime.timedelta(0, 0, 382331),
'encoding': None,
'headers': {'Date': 'Wed, 23 Jan 2019 17:43:09 GMT', 'Content-Length': '84', 'Content-Type': 'application/json', 'Connection': 'keep-alive'},
'history': [],
'raw': <urllib3.response.HTTPResponse object at 0x7f4a4dabced0>,
'reason': 'Internal Server Error',
'request': <PreparedRequest [POST]>,
'status_code': 500,
'url': u'https://api.meethue.com/oauth2/token?code=M8DkGE******&grant_type=authorization_code'
}

当我修改任何数据时(篡改随机数、错误的参数、错误的哈希......我收到 401 未经授权,或显示缺少数据的错误。但是当一切似乎都正常时,我得到了"invalid_request"并且无法继续使用令牌。

我在使用 Remote Hue API 时也遇到了问题。

我不是 python 开发人员,但我看到的是你不会向正文的输出流写入任何内容。我知道 - 身体是没有的 - 但似乎如果不写空字符串,你就不会得到一个令牌。似乎他们已经更新了一些他们的 Rest API。

我在实现中所做的:

final byte[] postData = "".getBytes(StandardCharsets.UTF_8);
connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, Integer.toString(postData.length));
connection.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
    wr.write(postData);
} catch (final Exception e) {
    LOG.error(METHOD + " Exception writing to outputstream of HttpConnection");
}

这解决了我的问题。一个建议 - 如果授权不起作用,请尝试基本身份验证版本 (https://developers.meethue.com/develop/hue-api/remote-authentication/)

最新更新