Advertisement
Guest User

Untitled

a guest
Feb 1st, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Collections.Specialized;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Diagnostics;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace GetWalletTransactions
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             // Based on "OAuth 2.0 for Mobile or Desktop Applications" from https://docs.esi.evetech.net/docs/sso/native_sso_flow.html
  19.  
  20.             // Step 2
  21.             const string clientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Dummy Client ID
  22.  
  23.             string authorizeUrl = @"https://login.eveonline.com/v2/oauth/authorize/";
  24.             const string callback = @"http://127.0.0.1:12500/callback/";
  25.  
  26.             // Prepare Code Challenge
  27.             Random random = new Random();
  28.             const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  29.             string codeChallenge = new String(Enumerable.Repeat(chars, 32).Select(s => s[random.Next(s.Length)]).ToArray());
  30.             string origEncodedCodeChallenge = urlSafeBase64Encode(codeChallenge);
  31.             string encodedCodeChallenge = ComputeSha256Hash(origEncodedCodeChallenge);
  32.             encodedCodeChallenge = urlSafeBase64Encode(encodedCodeChallenge);
  33.  
  34.             // Prepare Request for Authorization Code
  35.             string[] queryStringParameters =
  36.             {
  37.                 "response_type=code",
  38.                 "redirect_uri=" + HttpUtility.UrlEncode(callback),
  39.                 "client_id=" + clientId,
  40.                 "scope=" + HttpUtility.UrlEncode("esi-wallet.read_character_wallet.v1"),
  41.                 "code_challenge=" + encodedCodeChallenge,
  42.                 "code_challenge_method=S256",
  43.                 "state=" + HttpUtility.UrlEncode("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") // Dummy Secret
  44.             };
  45.  
  46.             string queryParameters = String.Join("&", queryStringParameters);
  47.  
  48.             authorizeUrl += "?" + queryParameters;
  49.  
  50.  
  51.             // Setup Listener for Authorization Code Response
  52.             HttpListener listener = new HttpListener();
  53.             listener.Prefixes.Add(callback);
  54.             listener.Start();
  55.             Console.Write("Listening...");
  56.  
  57.  
  58.             // Send Request for Authorization Code
  59.             Process.Start("chrome.exe", authorizeUrl);
  60.  
  61.             Console.WriteLine(listener.IsListening);
  62.  
  63.             HttpListenerContext context = listener.GetContext();
  64.             HttpListenerRequest request = context.Request;
  65.  
  66.             Uri uri = new Uri(request.Url.AbsoluteUri);
  67.             NameValueCollection parameters = HttpUtility.ParseQueryString(uri.Query);
  68.             Console.WriteLine("Returned string: {0}", uri.Query);
  69.  
  70.             string authCode = parameters["code"];
  71.             Console.WriteLine("Auth code: {0}", authCode);
  72.  
  73.  
  74.             // Prepare Request for Access Token
  75.             const string authBaseUrl = "https://login.eveonline.com/v2/oauth/token/";
  76.  
  77.             NameValueCollection post_params = new NameValueCollection();
  78.             post_params.Add("grant_type", "authorization_code");
  79.             post_params.Add("code", authCode);
  80.             post_params.Add("client_id", clientId);
  81.             post_params.Add("code_verifier", origEncodedCodeChallenge);
  82.            
  83.             using (WebClient client = new WebClient())
  84.             {
  85.                 client.Encoding = Encoding.UTF8;
  86.                 client.Headers.Clear();
  87.                 client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  88.                 client.Headers.Add("Host", "login.eveonline.com");
  89.  
  90.                 Console.WriteLine();
  91.                 Console.WriteLine($"Authorization URL: {authBaseUrl}");
  92.  
  93.                 try
  94.                 {
  95.                     byte[] response = client.UploadValues(authBaseUrl, "POST", post_params);
  96.                     Console.WriteLine(Encoding.UTF8.GetString(response));
  97.                 }
  98.                 catch (Exception ex)
  99.                 {
  100.                     Console.WriteLine(ex);
  101.                 }
  102.             }
  103.  
  104.             Console.ReadKey();
  105.         }
  106.  
  107.         public static string urlSafeBase64Encode(string input)
  108.         {
  109.             byte[] byteArray = Encoding.UTF8.GetBytes(input);
  110.             string working = Convert.ToBase64String(byteArray).TrimEnd('=');
  111.  
  112.             return working;
  113.         }
  114.  
  115.         // This function was taken from: https://www.c-sharpcorner.com/article/compute-sha256-hash-in-c-sharp/
  116.         public static string ComputeSha256Hash(string rawData)
  117.         {
  118.             using (SHA256 sha256Hash = SHA256.Create())
  119.             {
  120.                 // ComputeHash - returns byte array
  121.                 byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
  122.  
  123.                 // Convert byte array to a string
  124.                 StringBuilder builder = new StringBuilder();
  125.                 for (int i = 0; i < bytes.Length; i++)
  126.                 {
  127.                     builder.Append(bytes[i].ToString("x2"));
  128.                 }
  129.  
  130.                 return builder.ToString();
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement