[SalesForce] Wrong instance url while generating the token for OAuth 2.0 authentication

I am a novice in salesforce. I have access to multiple sandbox instances of salesforce. I am trying to do an OAuth2.0 authentication and call some rest api. But when I request for the token, in the response I am not getting the desired instance url. Any idea how to get the desired instance url?

Because that instance url will be the url for further rest api call that I want to perform.

I am using https://test.salesforce.com/services/oauth2/token to get the token.

Best Answer

You can call out to your own org's OAuth endpoints in the same way that you call out to the endpoints at test.salesforce.com and login.salesforce.com to get an access token. These are all valid endpoints and behave the same way:

Whichever endpoint you call out to will return the FQDN of the org as the instance_url value in the return payload. Since client IDs and secrets are unique across all orgs, you can call out your custom domain and login/test (depending on if your instance is a Production or Sandbox instance)

The only major consideration is for Government Cloud customers, who cannot reach their orgs through the generic login URLs and must use their custom domains.