Guides / n8n × Salesforce / Authentication

Why Salesforce OAuth breaks after a sandbox refresh

A refresh replaces the org. Refresh tokens, the app's Consumer Key, the integration user and often the My Domain host are all new — so the credential fails for four independent reasons at once.

Illustrated troubleshooting diagram for Why Salesforce OAuth breaks after a sandbox refresh.
invalid_grant — "expired access/refresh token" on a credential that worked yesterday, immediately after a sandbox refresh, with nothing changed on the integration side.
Short answer: A sandbox refresh does not update the sandbox — it replaces it with a fresh copy of production. The refresh tokens you held were issued by an org that no longer exists, the Connected App in the new sandbox is a new copy with its own Consumer Key and Secret, any integration user created by hand in the old sandbox is gone unless it also exists in production, and the sandbox name in the My Domain host may have changed. Re-authorize from scratch against the new org and verify with userinfo that you reached the sandbox and not production.

A refresh is a replacement, not an update

The word suggests a synchronisation and the reality is a rebuild. The sandbox org is discarded and recreated from production metadata, with a new org ID. Everything issued by or created in the previous copy is gone, which is why a credential that worked the previous evening fails the next morning with an error that reads like an ordinary expiry.

Because four things change simultaneously, fixing one at a time produces a sequence of different errors and the impression that the problem keeps moving. It is faster to treat the connection as new and rebuild it in one pass.

What a refresh changes

ThingWhat happensSymptom
Refresh tokenIssued by an org that no longer existsinvalid_grant at the next refresh
Connected AppFresh copy with a new Consumer Key/Secretinvalid_client_id, or a generic approval failure
Integration userGone unless it exists in productionLogin fails, or wrong visibility after re-auth
My Domain hostContains the sandbox name — may have changedAuthorization goes to a host that does not resolve
DataReplaced with a production copyRecord IDs your mapping stored no longer exist

Rebuilding the connection

Take the Consumer Key and Secret from the Connected App as it exists in the refreshed sandbox, not from your notes and not from production. A sandbox refresh copies the app, but the copy's Consumer Key belongs to the new org — reusing the production secret is a very common cause of a failure one step after the one you just fixed.

Authorize against the sandbox host. Every sandbox type authenticates through test.salesforce.com or through the sandbox's own My Domain, never through login.salesforce.com. The My Domain form is the production domain, two hyphens, the sandbox name, then .sandbox.my.salesforce.com — so a production org at acme.my.salesforce.com with a sandbox named uat authorizes at acme--uat.sandbox.my.salesforce.com.

The username carries the sandbox suffix too: a production user ada@example.com logs into the uat sandbox as ada@example.com.uat. Entering the production username produces an ordinary bad-credentials message that is easy to misread as an OAuth fault.

# Authorization and token endpoints — sandbox My Domain, typed explicitly
https://acme--uat.sandbox.my.salesforce.com/services/oauth2/authorize
https://acme--uat.sandbox.my.salesforce.com/services/oauth2/token
Scope: api refresh_token offline_access

# test.salesforce.com also works, routing by the username suffix:
#   https://test.salesforce.com/services/oauth2/authorize
#   username: ada@example.com.uat

# Then prove which org you actually reached — one request:
GET https://acme--uat.sandbox.my.salesforce.com/services/oauth2/userinfo
Authorization: Bearer ACCESS_TOKEN
# -> { "organization_id": "00D...",
#      "urls": { "custom_domain": "https://acme--uat.sandbox..." } }
#
# A sandbox org ID differs from production's. This is the check that
# prevents the worst outcome available here.

Never point a sandbox credential at production to save time

Tokens are scoped to the org that issued them, so at best a production token in a sandbox credential fails immediately. At worst — and this is the real risk — the credential ends up correctly configured for production while every person and every dashboard believes it points at the sandbox, and the next test run writes live customer data.

The userinfo check above takes one request and removes the possibility entirely. Make it a permanent step in the post-refresh runbook rather than something done when someone is suspicious.

The refreshed sandbox contains a copy of production data with new record IDs. Any mapping table holding sandbox record IDs from before the refresh now points at nothing, and a sync that trusts it will fail or write to the wrong records.

Make it a runbook, because it will happen again

Sandbox refreshes are scheduled, routine and usually performed by someone who is not the integration owner. Treating each one as an incident guarantees the same afternoon of confusion every time. Write down the sequence once: obtain the new Consumer Key and Secret, confirm the integration user exists in the refreshed org with the right profile and visibility, update the credential's authorization and token URLs if the sandbox name changed, re-authorize, run userinfo, then clear any cached record-ID mapping.

Two items are easy to forget. Long-lived workers that cached the old credential in an environment variable need restarting. And webhook or outbound-message endpoints configured in the old sandbox came from the production copy, so they may now point at production URLs or at nothing at all — check them before assuming the inbound path still works.

Ask to be told when a refresh is scheduled. Five minutes of notice converts this from an outage into a maintenance task.

  • New Consumer Key and Secret taken from the refreshed sandbox's app.
  • Authorization host is test.salesforce.com or the sandbox My Domain — never login.salesforce.com.
  • Username carries the sandbox suffix.
  • userinfo confirms a sandbox organization_id, not production's.
  • Cached record-ID mappings cleared; workers restarted.
  • Inbound webhook and outbound-message endpoints re-checked.

Reducing what a refresh can break

Some of the pain is avoidable by how the sandbox connection is set up in the first place. Keep sandbox and production credentials in entirely separate secret entries with names that make the environment obvious, so nothing can be copied from one to the other by habit. Point the sandbox credential at explicitly typed authorization and token URLs rather than relying on an environment toggle, so the host is visible in the configuration and can be updated deliberately when a sandbox name changes.

Create the integration user in production rather than by hand in the sandbox. A user that exists in production survives every refresh, because the refresh copies production; a user created directly in the sandbox is gone each time, and recreating it by hand reintroduces whatever profile and visibility differences you thought you had fixed.

Avoid storing sandbox record IDs anywhere durable. Treat sandbox data as disposable and rebuild any mapping the test run needs at the start of the run — a mapping table that outlives the org it refers to is worse than no mapping at all, because it fails by pointing at real records that happen to share an ID rather than by being empty.

Where these facts come from