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:
Create scratch org with sfdx commands.
Open scratch org and set a password for the user.
Reset security token.
Create a connected app, and copy the consumer key and consumer secret.
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;