Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  5. using Newtonsoft.Json;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     internal class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Started.");
  14.             httpClient.BaseAddress = new Uri($"https://fotoware--FluidoDev.my.salesforce.com");
  15.             var url = string.Format(UsernamePasswordUrlTemplate, ClientId, ClientSecret,
  16.                 WebUtility.UrlEncode("sarabjeet.pal@fotoware.com.fluidodev"), WebUtility.UrlEncode("Denmark!456"));
  17.            
  18.             Console.Write("Authentication with login sarabjeet.pal@fotoware.com.fluidodev and password Denmark!456... ");
  19.             var response = httpClient.PostAsync(url, null).Result;
  20.             var responseMessage = response.Content.ReadAsStringAsync().Result;
  21.             if (!response.IsSuccessStatusCode)
  22.                 throw new InvalidOperationException($"Occured exception: {responseMessage}");
  23.  
  24.             Console.WriteLine("ok");
  25.             var authenticationData = JsonConvert.DeserializeObject<OAuthResponse>(responseMessage);
  26.            
  27.             httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationData.AccessToken);
  28.            
  29.             Console.Write("/DupeChecker?email=john.smith@example.com ...");
  30.             var dupCheckerResult = httpClient.GetAsync(
  31.                 "services/apexrest/FotowareSubscriptionServices/DupeChecker?email=john.smith@example.com").Result;
  32.            
  33.             if (dupCheckerResult.IsSuccessStatusCode)
  34.                 Console.WriteLine("ok! 200");
  35.             else
  36.                 Console.WriteLine("not ok, status code " + dupCheckerResult.StatusCode);
  37.            
  38.            
  39.             Console.Write("/Trial ...");
  40.             var trialResult = httpClient.PostAsync(
  41.                 "services/apexrest/FotowareSubscriptionServices/Trial", new StringContent(JsonConvert.SerializeObject(new
  42.                 {
  43.                     Title = "",
  44.                     Telephone = "+47 00 00 00 00",
  45.                     Salutation = "Mr",
  46.                     FirstName = "John",
  47.                     LastName = "Smith",
  48.                     Industry = "Art",
  49.                     HostName = "hostname1.fotoware.com",
  50.                     EmailAddress = "john.smith1@example.com",
  51.                     Company = "Art Company Ltd."
  52.                 }))).Result;
  53.            
  54.             if (trialResult.IsSuccessStatusCode)
  55.                 Console.WriteLine("ok! 200");
  56.             else
  57.             {
  58.                 Console.WriteLine("not ok, status code " + dupCheckerResult.StatusCode);
  59.                 Console.WriteLine("Problem: \r\n" + trialResult.Content.ReadAsStringAsync().Result);
  60.             }    
  61.            
  62.            
  63.             Console.Write("/Accounts ...");
  64.             var accountsResult = httpClient.GetAsync(
  65.                 "services/apexrest/FotowareSubscriptionServices/Accounts").Result;
  66.            
  67.             if (accountsResult.IsSuccessStatusCode)
  68.                 Console.WriteLine("ok! 200");
  69.             else
  70.             {
  71.                 Console.WriteLine("not ok, status code " + dupCheckerResult.StatusCode);
  72.                 Console.WriteLine("Problem: \r\n" + trialResult.Content.ReadAsStringAsync().Result);
  73.             }  
  74.            
  75.             Console.WriteLine("Press any key");
  76.             Console.ReadKey();
  77.         }
  78.        
  79.         private const string ClientId =
  80.             "3MVG9GXbtnGKjXe5.8qcOjMRKshBAKPonkV2bADoV.V_a..eEWOdH233sXA5FqejgN2ufaILk0vBSUSWiL8k1";
  81.  
  82.  
  83.         private const string ClientSecret = "5217114035680202467";
  84.  
  85.        
  86.         private const string UsernamePasswordUrlTemplate = "services/oauth2/token?grant_type=password&client_id={0}&client_secret={1}&username={2}&password={3}";
  87.  
  88.         private static readonly HttpClient httpClient = new HttpClient();
  89.     }
  90.    
  91.     public class OAuthResponse
  92.     {
  93.         /// <summary>
  94.         /// Access token.
  95.         /// </summary>
  96.         [JsonProperty("access_token")]
  97.         public string AccessToken { get; set; }
  98.        
  99.         /// <summary>
  100.         /// Bound instance url.
  101.         /// </summary>
  102.         [JsonProperty("instance_url")]
  103.         public string InstanceUrl { get; set; }
  104.  
  105.         /// <summary>
  106.         /// Token id.
  107.         /// </summary>
  108.         [JsonProperty("id")]
  109.         public string Id { get; set; }
  110.  
  111.         /// <summary>
  112.         /// Token type, should be "Bearer"
  113.         /// </summary>
  114.         [JsonProperty("token_type")]
  115.         public string TokenType { get; set; }
  116.  
  117.         /// <summary>
  118.         /// ?
  119.         /// </summary>
  120.         [JsonProperty("issued_at")]
  121.         public string IssuedAt { get; set; }
  122.  
  123.         /// <summary>
  124.         /// ?
  125.         /// </summary>
  126.         [JsonProperty("signature")]
  127.         public string Signature { get; set; }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement