Consume Salesforce Rest API/Services


Consume Salesforce REST services

In this post, I am going to use the webrequest class to make the REST calls. Oaurth2 authentication is being used at salesforce side to validate the request. So my first request, will get the token from salesforce and the second request will get the actual data from salesforce.

Let’s get started

Step 1 - Create a new console application in visual studio 2013

Step 2 - Add below namespaces to your class

using System.IO;

using System.Runtime.Serialization.Json;

using System.Net;

Step 3 - The new version of salesforce using TSL 1.2, for that i have to set the correct security protocol

System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

Step 4 – Create request to get the token. We are setting the proxy in this request as we use the proxy settings to get the internet connectivity on production servers.

WebRequest loginReq = System.Net.WebRequest.Create(“Add salesforce login url”);

WebProxy myproxy = new WebProxy(“Proxy name without http:// or https://”, “Add proxy port”);

myproxy.Credentials = CredentialCache.DefaultCredentials;

loginReq.Proxy = myproxy;

loginReq.Method = "POST";

 

loginReq.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse loginResp = loginReq.GetResponse() as System.Net.HttpWebResponse;

                       Stream loginReceiveStream = loginResp.GetResponseStream();

// Pipes the stream to a higher level stream reader with the required encoding format.

StreamReader readStream = new StreamReader(loginReceiveStream, Encoding.UTF8);

                       string strLoginResult = readStream.ReadToEnd();

                       string strAuthToken = string.Empty;

//Parse AuthToken from result

                      strAuthToken = strLoginResult.Substring(strLoginResult.IndexOf("{\"access_token\":\"") + "{\"access_token\":\"".Length, strLoginResult.IndexOf("\",") - strLoginResult.IndexOf("{\"access_token\":\"") - "{\"access_token\":\"".Length);

Step 5 – Make the actual request to get the data from salesforce

WebRequest serviceReq = System.Net.WebRequest.Create(“Add Salesforce Rest URL”);

serviceReq.Method = "POST";

serviceReq.ContentLength = 0;

serviceReq.Headers.Add("Authorization", "Bearer " + strAuthToken);

serviceReq.Proxy = myproxy;

 

HttpWebResponse serviceResp = serviceReq.GetResponse() as System.Net.HttpWebResponse;

Stream serviceReceiveStream = serviceResp.GetResponseStream();

 

// Pipes the stream to a higher level stream reader with the required encoding format.

StreamReader serviceReadStream = new StreamReader(serviceReceiveStream, Encoding.UTF8);

 

string stResult1 = serviceReadStream.ReadToEnd();

Step 6 – Create the contract to de-serialize the salesforce response. You can get the response structure from salesforce developer.

    [DataContract]

    public class SalesforceResponse

    {

        [DataMember]

        public string status { get; set; }

 

        [DataMember]

        public List<Act> Act { get; set; }

    }

    [DataContract]

    public class attributes

    {

        [DataMember]

        public string type { get; set; }

 

        [DataMember]

        public string url { get; set; }

    }

[DataContract]

    public class Act

    {

        [DataMember]

        public attributes attributes { get; set; }

 

        [DataMember]

        public string Id { get; set; }

 

        [DataMember]

        public string Name { get; set; }

 

        [DataMember]

        public string RecordTypeId { get; set; }

 

        [DataMember]

        public string BillingCountry { get; set; }

 

        [DataMember]

        public string Finscan_Reporting_Required__c { get; set; }

 

    }

Step 7 - Desterilized the object using JavaScriptSerializer

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

serializer.MaxJsonLength = Int32.MaxValue;

SalesforceResponse deserializedObject = serializer.Deserialize<SalesforceResponse>(stResult1);

 

Comments

Popular posts from this blog

SSIS Merge Join - Both inputs of the transformation must contain at least one sorted column, and those columns must have matching metadata SSIS

jsGrid

Add Item to SharePoint List with attachment using client object model