question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

504 Error When Attempting To Make Remote API Calls

See original GitHub issue

Hi there. Thanks for the library, it has helped me build some really cool stuff and I appreciate all the work you’ve put in.

However, I’m attempting to connect remotely for the first time and I just can’t get things to work, despite pretty directly following the docs. Filing this here on the off-chance this is the library rather than the user.


Goal: Connect to a Hue Bridge via connectWithTokens() and make an API call Problem: Every API request returns a 504 error, even after a seemingly successful connectWithTokens() connection using just-generated credentials. Context: The Hue error docs and this tweet make me think a 504 error is the result of a bridge being not being connected to the internet. My understanding is that connectWithCode() only works if a connection to the bridge has been established.

As a further complication: I checked locally and I can see that no user associated with my new app which makes me think I’m missing something?


Flow That I Would Expect To Work

  1. Send visitor to authorization URL created via getAuthCodeUrl()
const remoteBootstrap = hue_api.api.createRemote(CLIENT_ID, CLIENT_SECRET);
document.location.href = remoteBootstrap.getAuthCodeUrl('my-app', APP_ID, STATE)
  1. In node catch callback from above URL and use it to connect to bridge via connectWithCode()
const remoteBootstrap = hue_api.api.createRemote(CLIENT_ID, CLIENT_SECRET);
remoteBootstrap.connectWithCode(authCode, USERNAME)
    .catch(err => {
         // Error stuff
  1. Then swap code for refresh and access tokens from bridge via getRemoteAccessCredentials()
      }).then(api => {
        const remoteCredentials = api.remote.getRemoteAccessCredentials();
        // Store remoteCredentials.tokens
        
        res.redirect(`app.com`)
     })
  1. Use those tokens to do anything you might with the local API via connectWithTokens()
const remoteBootstrap = hue_api.api.createRemote(CLIENT_ID, CLIENT_SECRET);
remoteBootstrap.connectWithTokens(ACCESS_VALUE, REFRESH_VALUE, USERNAME)
  .catch(err => {
     // Error stuff
  })
  .then(api => {
    // **This times out** 
    api.lights.getAll()
    .then(allLights => {
      // do something with allLights
    }
})

The first three steps are no problem but working with the API in step 4 results in an error every time 😕

Anything I’m overlooking? Am I misinterpreting remoteSetup.md?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
peter-murraycommented, Jan 11, 2021

It looks like something has changed somewhere (its been over a year since I last tested this). Now that I know the version I can puts some debugging time into it tomorrow 😄

0reactions
brokyocommented, Jan 16, 2021

Embarrassing! Yes, you’re absolutely right and my bridge was not connected to the portal and that was the source of the issue (I changed routers, didn’t reconnect, and the site gives unclear messaging 😰) this all works now. Thank you and, oof, apologies.


While reimplementing this I may have found a different bug, however. I’m not convinced passing a custom username to connectWithCode works

To Reproduce

Failed Process After getting auth code connect with:

const username = `foo`
remoteBootstrap.connectWithCode(authCode, username)
.then(api => {
   const remoteCredentials = api.remote.getRemoteAccessCredentials();
   // save username as `customUser`
   // save remoteCredentials.tokens as `tokens`
})

Then connect on an arbitrary API call:

remoteBootsrap.connectWithTokens(tokens.access.value, tokens.refresh.value, customUser)
.then(api => {
   api.lights.getAll()
   .then(lights => {
      // fail to do something with allLights
   })
})

When I do this I get ApiError: unauthorized user

Successful Process Whereas if I do the entire above process without passing in a username things work:

remoteBootstrap.connectWithCode(authCode)
.then(api => {
   const remoteCredentials = api.remote.getRemoteAccessCredentials();
   // save remoteCredentials.username as `defaultUser`
   // save remoteCredentials.tokens as `tokens`
})

remoteBootsrap.connectWithTokens(tokens.access.value, tokens.refresh.value, defaultUser)
.then(api => {
   api.lights.getAll()
   .then(lights => {
      // successfully do something with allLights
   })
})

It’s possible I’m missing something here but as I have no reason not to just use a default user haven’t dug too deep. Failed on a couple pass throughs so I thought I’d flag it 😃

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot API Gateway HTTP 504 timeout errors - AWS
To troubleshoot 504 timeout errors from API Gateway, first identify and verify the source of the error in your Amazon CloudWatch execution logs....
Read more >
504 Gateway Timeout from Backend Server | Apigee Edge
This error response indicates that the client did not receive a timely response from Apigee Edge or the backend server during the execution...
Read more >
How to Fix the 504 Gateway Timeout Error on Your Site - Kinsta
A 504 Gateway Timeout error indicates that the web server is waiting too long to respond from another server and “timing out.” There...
Read more >
The Quick & Easy Guide to Fixing 504 Gateway Timeout Errors
A 504 Gateway Timeout Error means your web server didn't receive a timely response from another server upstream when it attempted to load ......
Read more >
504 Gateway Timeout - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 504 Gateway Timeout server error response code indicates that the server, while acting as a gateway ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found