1

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 my Connected 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.

7
  • Can you confirm that the user has the API Enabled permission on their profile? Also, what scopes are you using in your connected app? Dec 8 '14 at 17:10
  • @MarkMasterson - How can I check if the user has API Enabled permission?
    – Jason W
    Dec 8 '14 at 17:17
  • 1. Check API Enabled Permision: Setup > Manage Users > Profiles > [Profile of the user in question] > Administrative Permission > API Enabled Dec 8 '14 at 17:19
  • Or if you have Enhanced Profile User Interface enabled: Setup > Manage Users > Profiles > [Profile of the user in question] > System Permissions > API Enabled Dec 8 '14 at 17:24
  • @MarkMasterson - The profile is System Administrator. It appears the API Enabled is set up
    – Jason W
    Dec 8 '14 at 17:25
2

From the description of the configuration settings on the Connected App, nothing seemed out of place:

  1. User was assigned the Connected App through a permission set.
  2. Consumer Key and Consumer secret were correct (as confirmed by you and a colleague).
  3. Connected App OAuth scopes are correct.
  4. User is API Enabled.

My recommended next step after checking these configurations was to try a security token refresh, and that seems to have done the trick. I wish I had a better explanation than sometimes Salesforce seems to be finicky with the security token, but it works often enough for me to be a sort of "Turn it off, and turn it on again"-type solution in my own troubleshooting steps. Perhaps someone else has better insight into the why of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.