python - 从 philips 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/)
推荐阅读
- twitter-bootstrap-3 - 无法使用 bootstrap 3 媒体查询删除所有小屏幕上的 div 填充
- ms-wopi - 如何识别 Office Online 365 中的刷新事件?
- angular - 更改路线后Angular 4脚本不起作用
- react-native - 文件下载不适用于 IOS 的本机反应
- c++ - 我想在 C++ 中用 openCV 拼接多个视频
- css - 如何使用 simple_form_for 在 Rails 应用程序中更改文本区域的大小和位置?
- java - 无法将照片从相机上传到服务器。IllegalArgumentException:列“_data”不存在
- erlang - 从 Elixir 调用 :binary.match Erlang 模块但遇到选项问题
- angular - Angular 理解属性绑定
- azure - 构建失败,“npm”未被识别为内部或外部命令