peter-murray / node-hue-api

Node.js Library for interacting with the Philips Hue Bridge and Lights
Apache License 2.0
1.18k stars 144 forks source link

`refreshTokens` failing because Content-Type defined twice in API request #201

Closed bladey closed 3 years ago

bladey commented 3 years ago

When using refreshTokens I get the following response back from Hue after debugging:

HttpError: HTTP error status: 400; {"fault":{"faultstring":"Duplicate Header \"content-type\"","detail":{"errorcode":"protocol.http.DuplicateHeader"}}}

These are the headers that are sent:

{
  baseURL: 'https://api.meethue.com',
  headers: {
    Accept: 'application/json',
    'content-type': 'application/x-www-form-urlencoded',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  },
  responseType: 'json'
}

This is in the config variable below:

image

At some point 2 content-type headers are inserted, I'm assuming if it only uses one the API call will start working.

Anyone else getting this issue?

peter-murray commented 3 years ago

Which version are you using to get this error? There were some changes to the underlying http library on the latest 5.x line so I need to know this to dial in on the fix.

bladey commented 3 years ago

Hey @peter-murray, I'm using version ^5.0.0-beta.2, thanks for the great library!

peter-murray commented 3 years ago

I think this should now be resolved in version 5.0.0-beta.3 which has just been published.

bladey commented 3 years ago

Hey @peter-murray, no longer getting the content-type error, but it seems like no data is coming back.

The error message is:

Error: Unexpected status code from refreshing tokens: 401

Here is how I'm calling refreshTokens:

const refreshToken = await api.remote.refreshTokens(HUE_REFRESH_TOKEN);

And here are the two config objects:

config {
  baseURL: 'https://api.meethue.com',
  headers: { Accept: 'application/json' },
  responseType: 'json'
}
requestConfig {
  url: '/oauth2/refresh',
  method: 'POST',
  data: 'refresh_token=LnITOPqV7ANhRtwdmTgqiBzi5uGCXVup', // example only
  params: { grant_type: 'refresh_token' },
  validateStatus: [Function: validateStatus]
}`

And the whole response:

res {
  status: 401,
  headers: [Object: null prototype] {
    date: [ 'Thu, 22 Jul 2021 23:07:21 GMT' ],
    'content-type': [ 'application/json' ],
    'content-length': [ '0' ],
    connection: [ 'close' ],
    'www-authenticate': [
      'Digest realm="oauth2_client@api.meethue.com", nonce="fb6dc533da596d158336b58c018f284d"'
    ]
  },
  data: ''
}

Ultimately resulting in Error: Unexpected status code from refreshing tokens: 401 as per the top of this message.

Config seems fine to me, but get a 401 no matter what.

Here is the entire code snippet just in case I'm missing something:

const remoteBootstrap = v3.api.createRemote(HUE_CLIENT_ID, HUE_CLIENT_SECRET);

remoteBootstrap
  .connectWithTokens(HUE_ACCESS_TOKEN, HUE_REFRESH_TOKEN, HUE_USERNAME)
  .then(async (api) => {
      const refreshToken = await api.remote.refreshTokens(HUE_REFRESH_TOKEN);
      console.log(refreshToken);
  });
peter-murray commented 3 years ago

Thank you for the details, it helped me diagnose this issue.

There were some endpoint changes for the OAuth2 from Hue that I was not aware of, but even then I had missed a refactoring of the swapping out of the HTTP library. the 401 error was associated with the digest challenge not being properly handled. Once that was cleared and the end points updated to the new documented /v2/oAuth variants, I have been able to refresh tokens locally in testing.

I have released version 5.0.0-beta.4 to the npm registry, can you please try that?

bladey commented 3 years ago

@peter-murray works now, thanks for the detailed answer.

I'm getting this as a response now (tokens changed for security) -

{
  accessToken: { value: 'HAubNWr5McxMlR4idiceXyD8vE6', expiresAt: NaN },
  refreshToken: { value: 'EPnsGkvd6fpiq5AHJocXdDrvuZlJxjxd', expiresAt: NaN }
}

It's coming back with NaN for expiresAt, but the tokens are successfully changing and old tokens are denied.

Thanks again for committing your time to this issue and the library in general, it is much appreciated.

peter-murray commented 3 years ago

Thank you for the feedback, there is a change to the returned payload that I have now captured and fixed, but will hold back on releasing util next week as I have other code changes in flight that I need to complete first.

bladey commented 3 years ago

Thanks @peter-murray, sounds great!