To get the Salesforce REST API works, the first thing we need to do is to create an app in Salesforce. Go to Setup page, on your left hand side, expand Create node under the Build section, then click Apps link.
Now on the main panel, there is a section named "Connected Apps", click the New button next to it.
On the next screen, firstly we need to fill in some basic information, as shown below
Then go to the API section, tick the checkbox "Enable OAuth Settings". Immediately there will be OAuth settings shown on the screen. To demonstrate the REST API, here I just make the callback URL as a test URL, and select full access as the authentication scope. In a real production environment, the access scope should be limited to specific account/group level.
Now once we save it, we should see the confirmation message as below
The activation of the app should not take too long, but meanwhile, we need to collect some information for the API coding task. Open the App and you should see the App details page like below. The consumer key and consumer secret are what we need for our coding task
Time to look at the coding side. In my Simpro post (part2) and Telstra post (here) I used webclient to post the message. Actually we can also use HttpClient to do the same task.
Assume what I want to do is to get a list of leads in Salesforce. The final output of the work looks like below.
Because we are going to use HttpClient, we need to add System.Net.Http into our reference list of the C# project.
Now the remaining coding is just like other REST API, obtain the access token, and submit the request, then receive the response. One thing could be important to you, is the version of your Salesforce when you build the query string. In a normal scenario, the version should not affect your query string. But it is always a good practise to check the version/document.
Code to access lead entity is shown below
private async void btnLogin_Click(object sender, EventArgs e)
{
HttpClient authClient = new HttpClient();
string ConsumerKey = "<ConsumerKey>";
string ConsumerSecret = "<ConsumerSecret>";
string Url = @"https://login.salesforce.com/services/oauth2/token";
string username = "<username>";
string password = "<password>";
string token = "<token>";
string loginpassword = password + token;
HttpContent httpContent = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "password"},
{"client_id", ConsumerKey},
{"client_secret", ConsumerSecret},
{"username", username},
{"password",loginpassword}
}
);
HttpRequestMessage Request = new HttpRequestMessage()
{
Method = HttpMethod.Post,
RequestUri = new Uri(Url),
Content = httpContent
};
var responsemessage = await authClient.SendAsync(Request);
var response = await responsemessage.Content.ReadAsStringAsync();
if (responsemessage.IsSuccessStatusCode)
{
var authToken = JsonConvert.DeserializeObject<object>(response);
string instance_url = ((JObject)authToken)["instance_url"].ToString();
string oauToken = ((JObject)authToken)["access_token"].ToString();
string sfQuery = instance_url + "/services/data/v34.0/query?q=SELECT+name+from+Lead";
HttpRequestMessage QueryRequest = new HttpRequestMessage(HttpMethod.Get, sfQuery);
QueryRequest.Headers.Add("Authorization", "Bearer " + oauToken);
var QueryResponse = await authClient.SendAsync(QueryRequest);
string QueryResponseResult = await QueryResponse.Content.ReadAsStringAsync();
JObject sfObject = JObject.Parse(QueryResponseResult);
JArray sfArray = (JArray)sfObject["records"];
foreach (JObject o in sfArray)
{
listBox1.Items.Add(o["Name"].ToString());
}
}
else
{
MessageBox.Show(response.ToString());
}
}
Very knowledgeable blog information about wholesale clothing, i like this.
ReplyDeleteSalesforce Melbourne