I am developing a C# console application that needs to connect to my company's Salesforce sandbox via the API to pull data. After some searching, I am using the nuget package DeveloperForce.Force to help with the connection.
I'm trying to use the OAuth username/password method to get my Access Token without success.
Here is the sample code taken from the Nuget Package project site that I am trying.
class Program
{
static void Main()
{
try
{
var task = RunSample();
task.Wait();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
var innerException = e.InnerException;
while (innerException != null)
{
Console.WriteLine(innerException.Message);
Console.WriteLine(innerException.StackTrace);
innerException = innerException.InnerException;
}
}
}
private static async Task RunSample()
{
var auth = new AuthenticationClient();
// Authenticate with Salesforce
Console.WriteLine("Authenticating with Salesforce");
var url = "https://test.salesforce.com/services/oauth2/token";
await auth.UsernamePasswordAsync("VALID_CLIENT_ID", "VALID_CLIENT_SECRET", "VALID_SALESFORCE_USER", "VALID_SALESFORCE_PASSWORD_AND_SECURITY_TOKEN, url);
Console.WriteLine("Connected to Salesforce");
var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
// retrieve all accounts
Console.WriteLine("Get Accounts");
const string qry = "SELECT ID, Name FROM Account";
var accts = new List<Account>();
var results = await client.QueryAsync<Account>(qry);
var totalSize = results.totalSize;
Console.WriteLine("Queried " + totalSize + " records.");
}
}
Inside the Nuget package code, the request is being made on line 68 with the correct values. However, this request is returning a 400 error saying that the password is invalid. (I have verified all the values are correct and gotten a colleague to confirm this).
In Salesforce I have done the following in hopes to resolve this
- Enabled my
Connected App
to my custom community - Created a new
Permission Set
and linked myConnected App
to the user directly
The Connected App
has the following scopes (likely overkill, but just trying to get it to work)
- Access and manage your data (api)
- Full access (full)
- Perform requests on your behalf at any time (refresh_token, offline_access)
- Provide access to custom applications (visualforce)
- Provide access to your data via the Web (web)
At this point, I'm thinking there is another setting within Salesforce that I have to set, but I cannot seem to find it. Any help is greatly appreciated.