[SalesForce] OAuth web server flow – login redirect loop

We have an OAuth Web Server Flow setup in our SF managed package that is used to authenticate our web service with Salesforce for API access. When a user first opens our app in SF, a call is first made to the /services/oauth2/authorize endpoint with the client's Org ID and our client details. Salesforce then normally redirects the user to their remote access authorization page, asking the user to grant our app permission to access their org.

It seems that for a small few of our clients that are attempting to use our app within a sandbox, they get redirected to the Salesforce login page at test.salesforce.com, and entering their credentials simply logs them into Salesforce. Our callback URL is never sent the appropriate auth code.

It occurs somewhat rarely, as some sandboxes authorize without any issues.

Anyone have any ideas on how to diagnose this, or what might be happening?

Best Answer

This is a problem with the salesforce session negotiation on sandbox orgs that use My Domain. There is no fix but a workaround is possible:

  1. upon arriving at the login page, don't login, but view the page source,

  2. locate the hidden input tag and value attribute by searching for "r=,

  3. copy the whole string after the = and paste it into an RFC URL decoder,

You now have the redirect URL that was supposed to go back to the org. And, regardless of having other browser sessions open or not (even in a chrome incognito window) one will see that the pod is wrong, for example it may show cs30 or cs17 even if your pod is cs1 in actuality.

Replace the wrong pod with the right pod, and paste that amended URL into the browser for it to complete the dance and return you into salesforce.

Related Topic