Search code examples
salesforcesalesforce-lightningsalesforce-communities

How to access multiple salesforce orgs with same connected app


I am trying to access the multiple communities in multiple salesforce sites with same connected-app but could not.

For example: I have created the two salesforce sites A and B. And created connected-app in A. To access rest API of A, i could authenticate with connected-app in A, But how to connect with B with using the same client_secret and client_id by differs in user_name and password? Or is there any way to connect with REST API with username and password as creds?

Example: There is community_A1, community_A2 in Site A and community_B1 , community_B2 in Site B. I need to access communities in both the Sites


Solution

  • This should work out of the box, what errors you're getting? You can have 1 set of consumer key and secret and (if admin in target org didn't block it) you could use this to login to any SF in the world, sandbox or production, core SF or community.

    Imagine being a developer of mobile app that pulls data from salesforce. You'd want to have just 1 pair of keys, not install separate key for every org people might install your app...

    All org-specific stuff you need is username, password and login url. For community the login url will look like https://sandboxName-MyDomain.cs123.force.com/communityName

    Once you have that, you can inspect all the OAuth2 stuff at https://sandboxName-MyDomain.cs123.force.com/communityName/.well-known/openid-configuration

    Have you played with https://openidconnect.herokuapp.com/ ? Paste your community url, for now keep the client and secret as is, click next. Login to community and keep clicking next. It should work fine and at the end - give you access_token / session id.

    Now close this browser completely / open new incognito window and do it again. This time give your login url but also client and secret. Next, Next... Check the user's login history, it should mention your connected app now.

    Now close this and do it last time, with the other community's login url and another user's credentials but still with your client and secret.


    is there any way to connect with REST API with username and password as creds?

    Not for community. Internal users can get access in one go with https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_username_password_flow.htm&type=5 but community/experience users need this interactive login, there has to be a human consent / password typing step. And anyway it's not very secure flow. "Experience Cloud sites don’t support the OAuth 2.0 username-password flow."

    In a pinch you could try the SOAP API's login method but from what I remember you may need to pass the community's Id in the message then