I am trying to get access token using OAuth for a salesforce account.I have Consumer Key and Consumer Secret Below is my code. But it throws an exception when it calls the method UsernamePasswordAsync. Please help me around this issue.
private static async Task RunSample()
{
try
{
var auth = new AuthenticationClient();
// Authenticate with Salesforce
Console.WriteLine("Authenticating with Salesforce");
var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase)
? "https://login.salesforce.com/services/oauth2/token "
: "https://test.salesforce.com/services/oauth2/token";
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url);
Console.WriteLine("Connected to Salesforce");
var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
// retrieve all accounts
Console.WriteLine("Get Accounts");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
EDIT: The exception I am getting is Exception: authentication failure Error:Salesforce.Common.Models.Error.InvalidGrant Stack Trace: at Salesforce.Common.AuthenticationClient.d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at SalesForceAuthSample.Program.d__7.MoveNext() in C:\Users\Pankaj Dalvi\Documents\Visual Studio 2015\Projects\MVP\SalesForceAuthSample\SalesForceAuthSample\Program.cs:line 65