SFDX Unable to Authorize Org

Overview

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.

http://localhost:1717/OauthRedirect?code=jedeiojropu2iou23u2io3uj

If you want to modify the port that the request should redirect, then on your sfdx-project.json add the oauthLocalPort key.

"namespace": "",
    "sfdcLoginUrl": "https://login.salesforce.com",
    "sourceApiVersion": "50.0",
    "oauthLocalPort": 7171

How to Fix this error

I logged out of all my Orgs on VS Code. Go to the command palette and look for:

SFDX: Log out of all Authorized Orgs

A quick search on the web and I stumbled on this stack exchange post, which was extremely helpful.

Stake Exchange Article

Authorize a Device

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.

sfdx force:auth:device:login -r https://test.salesforce.com

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.

sfdx force:config:set defaultusername=testuser@test.com

Note: the username must match the username that you use on the step Authorize a Device.

Leave a comment