Before you start developing with Salesforce DX, I recommend that you set your org and default username. The setdefaultusername parameter sets all Salesforce projects to use this org/scratch-org when using sfdx commands. This command is very helpful if you support multiple Salesforce projects.
Recently, I ran into this error while refreshing my Oauth Token on VS Code. I tried to re-authorize the org and it failed. I had never encounter this type of failure. Usually, clearing out the .sfdx/<username>.json file and reauthorizing the org worked. This time it was different.. The error lingered and I had no additional information to debug. My next step was to research and troubleshoot.
Did I mention that I run Linux?
I recently had applied a bunch of package updates to my linux desktop and thought that my firewall was blocking tcp traffic into port 1717. I did a quick check and that was not the case. BTW – the local nodejs server runs only while the OAUTH workflow is in progress. Once the flow completes, the local server is shutdown.
Salesforce provides many secure flows for authorizing applications and devices. In this case, authorizing my desktop (device) was the best option. First, run the following command on the terminal in VS Code. Or open a terminal and go to sfdx project directory.
Make sure that you specify -r or –instanceurl flag. Sandboxes use the test.salesforce.com.
The outcome of a successful authenticated device follows.
▶ sfdx force:auth:device:login -r https://test.salesforce.com
=== Action Required!
Enter <CODE> user code in the verification URL https://test.salesforce.com/setup/connect
Login successful for testuser@test.com. You can now close the browser.
Final Step
In VS Code, command palette, select SFDX:Authorize an Org. You should see your org listed in the command box. Select the org and you are all set.
Just in case, here’s the command that VS Code runs behind the scenes. You can run the command in liue of the VS Code approach.