I'm having some trouble getting the REST API to work on a scratch org. When I call the login endpoint I get the error "invalid grant". (This happens when I try test.salesforce.com as well as login.salesforce.com.) My code works fine with logins for non-scratch orgs; it's only my scratch org that causes this error.

Here are the steps I've followed:

  1. Create scratch org with sfdx commands.

  2. Open scratch org and set a password for the user.

  3. Reset security token.

  4. Create a connected app, and copy the consumer key and consumer secret.

  5. Edit the connected app policy and relax IP restrictions.

When I perform these steps and put all the required values in my request, it works fine if I'm using a non-scratch org, but I get an "invalid grant" error for a scratch org.

What steps am I missing? What is it about scratch orgs that makes this not work?

EDIT: Here is the relevant C# code:

public SalesforceClient Login()
            SalesforceClient sfClient = new SalesforceClient();
            String jsonResponse;
            using (HttpClient client = new HttpClient())
                HttpContent request = new FormUrlEncodedContent(new Dictionary<string, string>
                        {"grant_type", "password"},
                        {"client_id", sfClient.ClientId},
                        {"client_secret", sfClient.ClientSecret},
                        {"username", sfClient.Username},
                        {"password", sfClient.Password + sfClient.Token}
                request.Headers.Add("X-PrettyPrint", "1");
                HttpResponseMessage response = client.PostAsync(sfClient.LOGIN_ENDPOINT, request).Result;
                jsonResponse = response.Content.ReadAsStringAsync().Result;

1 Answer 1


I figured it out. All I had to do was remove the token from the password field. The token is not required at all. I have no idea why this is the case, so if anyone has an explanation, please let me know!

  • The token is required when you are performing call outside IP ranges. As you relaxed IP restriction may be this is the cause. Commented Jan 9, 2019 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .