Wednesday, June 5, 2013

SalesForce REST API sample using VB.Net

This is in continuation from my previous post where I had explained how to fetch data from SalesForce using the SOAP API. Now we are going to see how to do a call using REST API.

Prerequisites:

User Name
Password
Security Token (needed for the Api calls as the password is considered as password+security token)
Client Secret (Required for REST Call)
Client Id (Required for REST Call)
Call Back (This is required if we are authenticating from the web, else if we have a console application this could be some dummy value)


Steps:

1.     Create a console application
2.     In the main class (Program.vb) add the below methods
3.     Add a class called TokenResponse as indicated below
4.     Run the console application , if everything is right you should have your results as JSON ..




Sample Code:



''' <summary>
''' Main method which performs the call to the REST service
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Sub queryUsingRest()
Dim clientID As String = "clientID"
Dim clientSecret As String = "clientSecret"
Dim redirectURL As String = "callback:none"
Dim username As String = "username"
Dim password As String = "password"
Dim securityToken As String = "securityToken"
Dim URI As String = https://login.salesforce.com/services/oauth2/token
Dim body As StringBuilder = New StringBuilder
body.Append("grant_type=password&")
body.Append(("client_id=" _
+ (clientID + "&")))
body.Append(("client_secret=" _
+ (clientSecret + "&")))
body.Append(("username=" _
+ (username + "&")))
body.Append(("password=" _
+ (password + securityToken)))


'Post to authenticate and get the service token  Dim result As String = HttpPost(URI, body.ToString)


'DeSerialize the JSON request to get the access_toen and instance url
Dim ser As JavaScriptSerializer = New JavaScriptSerializer
token = ser.Deserialize(Of TokenResponse)(result)

'Create the REST request
Dim query As String = "?q=SELECT Name, Id from Account"

'Query using rest
Dim results = HttpGet(token.instance_url + "/services/data/v27.0/query" + query, "")

Console.WriteLine("Results from the query - ")
Console.WriteLine(results)

End Sub


''' <summary>
''' Used to perfrom the HTTP GET
''' </summary>
''' <param name="URI"></param>
''' <param name="Parameters"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function HttpGet(ByVal URI As String, ByVal Parameters As String) As String
Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(URI)
req.Method = "GET"
req.Headers.Add(("Authorization: OAuth " + token.access_token))
Dim resp As System.Net.WebResponse = req.GetResponse
If (resp Is Nothing) Then
Return Nothing
End If
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(resp.GetResponseStream)
Return sr.ReadToEnd.Trim
End Function


''' <summary>
''' Used to post the HTTP request
''' </summary>
''' <param name="URI"></param>
''' <param name="Parameters"></param>
''' <returns></returns>
''' <remarks></remarks> 
Public Function HttpPost(ByVal URI As String, ByVal Parameters As String) As String

Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(URI)
req.ContentType = "application/x-www-form-urlencoded"
req.Method = "POST"
' Add parameters to post
Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(Parameters)
req.ContentLength = data.Length

Dim os As System.IO.Stream = req.GetRequestStream
os.Write(data, 0, data.Length)
os.Close()
 

'Do the post and get the response.
Dim resp As System.Net.WebResponse = req.GetResponse
If (resp Is Nothing) Then
Return Nothing
End If
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(resp.GetResponseStream)
Return sr.ReadToEnd.Trim
End Function


_____________________________________________________________________________________

Add this class separately as this is used to Deserialize the Rest request



 ''' <summary>
''' Class used to deserialize the response object from
''' the REST call
''' </summary>
''' <remarks></remarks>
Public Class TokenResponse

Public id As String
Public issued_at As String
'Public refresh_token As String
Public instance_url As String
Public signature As String
Public access_token As String
End Class
















No comments:

Post a Comment