I am working with a managed package that is installed in Org A. The user wants to be able to access information from their other org, Org B, where this package is not installed. The plan is to use web server oauth flow to grant access to the managed package and return the user to their originating org.
As a proof of concept I'm doing the following:
- Log into Org A
- Manually navigate to https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=[MyClientId]&redirect_uri=http%3A%2F%2Fna1.salesforce.com%2Fapex%2FExternalOAuthHandler
- Log in to Org B and approve access to the connected app.
- Grab the "code" parameter off of the query string and execute a POST request to get the access token and instance url for Org B.
Here's some sample code for what i'm posting for step 4 (demo only - no way would you put the client_id and secret here):
$.post("https://login.salesforce.com/services/oauth2/token", {
data: {
"grant_type": "authorization_code",
"code": "[code from first request]",
"client_id": "[MyClientId]",
"secret": "[MySecret]",
"redirect_uri", "https://na1.salesforce.com/apex/ExternalOAuthHandler"
}
}).done(function(response) {
console.log(response);
});
There are 2 issues I'm running into:
- The callback url that I configure for my connected app will be in an unpredictable domain for each customer that wants to use this feature. I won't know their instance and they could also have it installed in an org with My Domain enabled. The callback url can't contain wildcards either. If I use na1 for the domain, i.e. http://na1.salesforce.com/apex/ExternalOAuthHandler, then it will look for that page in Org B where it doesn't exist. So using na1 is definitely not an option. If I just pass whatever the originating host is and append "/apex/ExternalOAuthHandler", I receive a callback mismatch error because it will be unpredictable and won't match the configured callback url. How can I resolve this?
- Even though I don't redirect to the correct URI with the na1 url, I do still get the code on the query string and a page not found message in OrgB. I can still grab that code and perform step 4 manually. However, I receive the following response:
{ "error": "invalid_client", "error_description": "invalid client credentials" }
Why would I get invalid client credentials here? Am I going about this the right way and missing something?
The tutorial I followed is here: https://www.youtube.com/watch?v=vlrK3YZ_Fj0. However the difference with my scenario is that I'm getting this to work with a managed package installed in someone's org where the domain of the redirect URL is unpredictable.