Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Security.Cryptography;
- namespace SeniorIT.ServerAdmin.ServiceAgents
- {
- public class QuickBloxAgent
- {
- public static string GetToken()
- {
- HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("https://api.quickblox.com/session.xml");
- string application_id = "xxxx";
- string auth_key = "xxxxxxxxx";
- string auth_secret = "xxxxxxx";
- ASCIIEncoding encoding = new ASCIIEncoding();
- string postData = "application_id=" + application_id;
- postData += "&auth_key=" + auth_key;
- postData += "&nonce=" + GetNonce();
- postData += "×tamp=" + GetTimestamp();
- string signature = GetHashString(auth_secret, postData);
- postData += "&signature=" + signature;
- byte[] data = encoding.GetBytes(postData);
- httpWReq.Method = "POST";
- httpWReq.ContentType = "application/x-www-form-urlencoded";
- httpWReq.ContentLength = data.Length;
- httpWReq.Headers["QuickBlox-REST-API-Version"] = "0.1.0";
- using (Stream stream = httpWReq.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- try
- {
- HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
- string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
- return responseString;
- }
- catch (Exception ex)
- {
- string err = ex.Message;
- return null;
- }
- }
- private static int GetNonce()
- {
- return new Random().Next(1000, 9999);
- }
- private static string GetTimestamp()
- {
- long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
- ticks /= 10000000;
- return ticks.ToString();
- }
- private static byte[] GetHash(string inputString)
- {
- HashAlgorithm algorithm = HMACSHA1.Create();
- return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
- }
- private static string GetHashString(string secret, string postdata)
- {
- StringBuilder sb = new StringBuilder();
- foreach (byte b in GetHash(string.Format("{0}{1}", secret, postdata)))
- {
- sb.Append(b.ToString("X2"));
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement