从philips hue远程API获取令牌时出错

问题描述 投票:0回答:1

我正在关注远程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*************, [[email protected]](mailto:[email protected]), 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”并且无法继续使用令牌。

python oauth-2.0 token philips-hue
1个回答
0
投票

我也遇到了Remote Hue API的问题。

我不是python dev,但我看到的是你没有向body的输出流写任何东西。我知道 - 身体没有 - 但似乎没有写一个空字符串,你就不会得到一个令牌。似乎他们已经更新了他们的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/

© www.soinside.com 2019 - 2024. All rights reserved.