12

I think my access token for my scratch org expired. I'm using sfdx in visual studio code. What's the easiest way to get/fresh my access token?

6 Answers 6

11

If reauthorizing doesn't help for some reasons(as I was facing) try deleting the alias as follows. It should definitely work

Step 1. Go to /home/.sfdx (In windows c:\user\.sfdx)

Step 2. Delete org specific json file(ORG_USERNAME.json).

Step 3. Edit alias.json file and remove the alias.

Step 4. Restart VS Code and reauthorize.

1
  • I was facing same issue. sfdx force:org:list had expired token, sfdx force:auth:web:login refreshed it, but couldn't log in. This clear then sfdx force:auth:web:login worked well for me. Sep 1, 2021 at 15:35
6

The easiest way to refresh the access token is just to open the scratch org via Salesforce DX.

From the command palette, you can do > SFDX: Open Default Org. Or, from the integrated terminal, do

sfdx force:org:open

optionally with -u my_scratch_alias to select a specific org.

Once you do so, any other functionality you might use that depends on getting an access token via SFDX should work again, provide the scratch org is not expired.

5
  • 2
    This is what i am trying to do when i get the error
    – Ben Arnao
    Mar 25, 2019 at 15:20
  • 1
    Are you certain that the scratch org is not expired? Could you show in your question the output of sfdx force:org:list?
    – David Reed
    Mar 25, 2019 at 15:22
  • I guess it did expire.. thanks
    – Ben Arnao
    Mar 25, 2019 at 15:25
  • What about CI / CLI tools? Apr 10, 2019 at 8:26
  • This is happening every other day to me now. The org does not appear in force:org:list anymore, nor does org:open help. When I do a force:org:delete this command works - but I cannot access the scratch org anymore :( (Also tried manual login) Apr 3, 2020 at 5:34
6

These steps in Visual Studio Code will fix the issue,

Step1: In the command palette SFDX: Authorize an Org

Step2: Enter alias name (example: lwc_sandbox)

Step3: Login into your Org from the default browser

Authorization Success and Token expired issue will be fixed.

4

Today I was facing the same issue and I am using vscode on Ubuntu 20. I don't know if it's a bug on vscode or what, but what I did was re-authenticate (SFDX: Authorize and Org) then closed the app and opened again and now it's working. Because I was re-authenticating and not closing the app it wasn't working and I kept re-authenticating a couple more times until I decided to close the app and open again. I hope it helps!

2
  • please read my latest answer, it might solve your problem Jan 11 at 12:59
  • Restarting VS Code on Windows also solved my problems. Jan 12 at 10:57
3

Again log in using force:auth:web:login:

sfdx force:auth:web:login -d -a <alias> -r [<https://test.salesforce.com>,<https://login.salesforce.com>]
  • -d will set the authenticated org as the default Dev Hub org.
  • -a sets the alias for the authenticated org (e.g. "qa", "uat", "dev")
  • -r or --instanceurl is the URL you want to authenticate with.
3

ABSOLUTE SOLUTION

It is because you might have Authorized the same org twice or more with the same username and alias,

VS Code is a static entity that picks auth info from your folder: c:\user\.sfdx, and it will try to re-auth from old info (if it founds multiple)

that's why it is giving the auth error every time or sometimes (behavior is irregular).

solution:

please resolve all duplicates from your folder "c:\user\.sfdx" and they try again. it will work definitely.

PS: Files need to modify

  1. alias - remove all multiple aliases if any
  2. remove all username.json files from this folder
  3. Try to re-authenticate org

Enjoy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.