Advertisement
Guest User

Untitled

a guest
Jan 17th, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8.  
  9. namespace SeniorIT.ServerAdmin.ServiceAgents
  10. {
  11.     public class QuickBloxAgent
  12.     {
  13.         public static string GetToken()
  14.         {
  15.             HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("https://api.quickblox.com/session.xml");
  16.  
  17.             string application_id = "xxxx";
  18.             string auth_key = "xxxxxxxxx";
  19.             string auth_secret = "xxxxxxx";
  20.            
  21.             ASCIIEncoding encoding = new ASCIIEncoding();
  22.             string postData = "application_id=" + application_id;
  23.             postData += "&auth_key=" + auth_key;
  24.             postData += "&nonce=" + GetNonce();
  25.             postData += "&timestamp=" + GetTimestamp();
  26.  
  27.             string signature = GetHashString(auth_secret, postData);
  28.             postData += "&signature=" + signature;
  29.             byte[] data = encoding.GetBytes(postData);
  30.  
  31.             httpWReq.Method = "POST";
  32.             httpWReq.ContentType = "application/x-www-form-urlencoded";
  33.             httpWReq.ContentLength = data.Length;
  34.             httpWReq.Headers["QuickBlox-REST-API-Version"] = "0.1.0";
  35.  
  36.             using (Stream stream = httpWReq.GetRequestStream())
  37.             {
  38.                 stream.Write(data, 0, data.Length);
  39.             }
  40.  
  41.             try
  42.             {
  43.                 HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
  44.  
  45.                 string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  46.  
  47.                 return responseString;
  48.             }
  49.             catch (Exception ex)
  50.             {
  51.                 string err = ex.Message;
  52.  
  53.                 return null;
  54.             }
  55.         }
  56.  
  57.         private static int GetNonce()
  58.         {
  59.             return new Random().Next(1000, 9999);
  60.         }
  61.  
  62.         private static string GetTimestamp()
  63.         {
  64.             long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
  65.             ticks /= 10000000;
  66.  
  67.             return ticks.ToString();
  68.         }
  69.  
  70.         private static byte[] GetHash(string inputString)
  71.         {
  72.             HashAlgorithm algorithm = HMACSHA1.Create();
  73.  
  74.             return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
  75.         }
  76.  
  77.         private static string GetHashString(string secret, string postdata)
  78.         {
  79.             StringBuilder sb = new StringBuilder();
  80.  
  81.             foreach (byte b in GetHash(string.Format("{0}{1}", secret, postdata)))
  82.             {
  83.                 sb.Append(b.ToString("X2"));
  84.             }
  85.  
  86.             return sb.ToString();
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement