Verify the REST Environment (REST-Based APIs)
You can verify your developer environment with a simple C# test application in Visual Studio.
You must have Visual Studio installed to create a C# test application.
-
Start Visual Studio.
-
Click File | New Project.
-
In the New Project window, navigate to Installed | Templates | Visual C# | Windows. Select Console Application.
-
In the New Project window, under Name, name the project VerifyRESTTest. Under Solution name, name the solution VerifyRESTTest. Under Location, pick a file location that you’ll remember. Click Ok. A solution is created, and Program.cs is opened for editing.
-
Click Project | Add Reference.
-
In the Reference Manager window, navigate to Assemblies | Framework. Select System.Runtime.Serialization. Ensure that the checkbox is selected, and click Ok.
-
Replace the code in Program.cs as described in the following section.
Use the following simple login example code for your Program.cs file. Replace YOUR DEVORG USERNAME with your developer organization username, and replace YOUR DEVORG PASSWORD + SECURITY TOKEN with your developer organization password appended with your security token. If you did not set a security token in your organization, just provide your password. Replace YOUR OAUTH CONSUMER KEY with the consumer key from your development organization’s connected app. Replace YOUR OAUTH CONSUMER SECRET with the consumer secret from your development organization’s connected app. A GitHub Gist of this code is available here: https://gist.github.com/anonymous/db367194e6e24faa081b.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Net;
7using System.Runtime.Serialization;
8using System.Runtime.Serialization.Json;
9
10namespace VerifyRESTTest
11{
12 class Program
13 {
14 // Class used for serializing the OAuth JSON response
15 [DataContract]
16 public class OAuthUsernamePasswordResponse
17 {
18 [DataMember]
19 public string access_token { get; set; }
20 [DataMember]
21 public string id { get; set; }
22 [DataMember]
23 public string instance_url { get; set; }
24 [DataMember]
25 public string issued_at { get; set; }
26 [DataMember]
27 public string signature { get; set; }
28 }
29
30 private static string accessToken = "";
31 private static string instanceUrl = "";
32
33 private static void login()
34 {
35 string acctName = "YOUR DEVORG USERNAME";
36 string acctPw = "YOUR DEVORG PASSWORD AND SECURITY TOKEN";
37 string consumerKey = "YOUR OAUTH CONSUMER KEY";
38 string consumerSecret = "YOUR OAUTH CONSUMER SECRET";
39
40 // Just for testing the developer environment, we use the simple username-password OAuth flow.
41 // In production environments, make sure to use a stronger OAuth flow, such as User-Agent
42 string strContent = "grant_type=password" +
43 "&client_id=" + consumerKey +
44 "&client_secret=" + consumerSecret +
45 "&username=" + acctName +
46 "&password=" + acctPw;
47
48 string urlStr = "https://login.salesforce.com/services/oauth2/token?" + strContent;
49
50 HttpWebRequest request = WebRequest.Create(urlStr) as HttpWebRequest;
51 request.Method = "POST";
52
53 try
54 {
55 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
56 {
57 if (response.StatusCode != HttpStatusCode.OK)
58 throw new Exception(String.Format(
59 "Server error (HTTP {0}: {1}).",
60 response.StatusCode,
61 response.StatusDescription));
62
63 // Parse the JSON response and extract the access token and instance URL
64 DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(OAuthUsernamePasswordResponse));
65 OAuthUsernamePasswordResponse objResponse = jsonSerializer.ReadObject(response.GetResponseStream()) as OAuthUsernamePasswordResponse;
66 accessToken = objResponse.access_token;
67 instanceUrl = objResponse.instance_url;
68 }
69 }
70 catch (Exception e)
71 {
72 Console.WriteLine("\nException Caught!");
73 Console.WriteLine("Message :{0} ", e.Message);
74 }
75 }
76
77 static void Main(string[] args)
78 {
79 login();
80 if (accessToken != "")
81 {
82 // display some current login settings
83 Console.Write("Instance URL: " + instanceUrl + "\n");
84 Console.Write("Access Token: " + accessToken + "\n");
85 Console.Write("Press any key to continue:\n");
86 Console.ReadKey();
87 }
88 }
89 }
90}
The following example output shows a typical successful run of this code.
1Instance URL: https://yourInstance.salesforce.com
2Access Token: 00DU0000000L3f0!ARoAQKl4rimMZ7kh11QpvHv....VreCvYJafmsCd2MgBM1UltPflQ
3Press any key to continue:
If the verification C# project runs and displays output that matches your organization, your developer environment is set up and you can start developing .NET applications that integrate with Salesforce REST APIs.