0

I'm following the remote api docs (https://developers.meethue.com/develop/hue-api/remote-authentication/), but i'm getting a server error when requesting the token via digest authentication.

I'm building the request in python (also tried php and 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)

The request and response sent to meethue.com are the following:

('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'
}

When i modify any data (tamper the nonce, wrong parameter, wrong hashes...) I get a 401 unauthorized, or an error showing missing data. But when everything seems to be OK, i got the "invalid_request" and cant go on with the token.

fsaravia
  • 31
  • 3

1 Answers1

0

I had also a problem with the Remote Hue API.

I'm not a python dev but what I see is that you don't write anything to the outputstream of the body. I know - the body is none - but it seems that without writing an empty string, you don't get a token. Seems they have updated a bit their Rest API.

What I have done in my implementation:

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");
}

This solved my problem. One suggestion - in case the authorization does not work try the basic auth version (https://developers.meethue.com/develop/hue-api/remote-authentication/)

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
snowflake
  • 83
  • 1
  • 6