【问题标题】:Getting token with Quickblox in C#在 C# 中使用 Quickblox 获取令牌
【发布时间】:2018-01-18 15:08:23
【问题描述】:

我正在尝试访问我的 QuickBlox 应用并使用 REST API 获取令牌。

我的代码如下所示: http://pastebin.com/rp2KLMp2

请求如下所示(敏感信息已删除): application_id=xxxx&auth_key=xxxxxxxxxx&nonce=2851&timestamp=1389951758&signature=D481F13E87F47D4C17697EF9D2C8E25777E09079

我收到错误: 远程服务器返回错误:(422) Unprocessable Entity

可能是什么问题?

【问题讨论】:

    标签: quickblox


    【解决方案1】:
        public  string Timestamp()
        {
            long ticks = DateTime.UtcNow.Ticks - 
            DateTime.Parse("01/01/1970 00:00:00").Ticks;
            ticks /= 10000000;
            return ticks.ToString();
        }
        public  string Hash(string input, string key)
        {
            var encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(key);
            HMACSHA1 myhmacsha1 = new HMACSHA1(keyByte);
            byte[] byteArray = Encoding.ASCII.GetBytes(input);
            MemoryStream stream = new MemoryStream(byteArray);
            byte[] hashValue = myhmacsha1.ComputeHash(stream);
            return string.Join("", Array.ConvertAll(hashValue, b => b.ToString("x2")));
        }
        public string GetToken()
        {
            if (HttpContext.Current == null || String.IsNullOrEmpty(Convert.ToString(HttpContext.Current.Cache["QuickBloxToken"])))
            {
            string url = "https://api.quickblox.com"; //ConfigurationManager.AppSettings["ChatUrl"].ToString();
            HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url + "/session.xml");
    
            httpWReq.UserAgent = ".NET Framework Test Client";
    
            string application_id = System.Configuration.ConfigurationManager.AppSettings["QuickApplication_id"].ToString();
            string auth_key = System.Configuration.ConfigurationManager.AppSettings["QuickAuth_key"].ToString();
            string timestamp = Timestamp();
            string auth_secret = System.Configuration.ConfigurationManager.AppSettings["QuickAuth_secret"].ToString();
    
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "application_id=" + application_id;
            postData += "&auth_key=" + auth_key;
    
    
            Random rand = new Random();
    
            postData += "&nonce=" + rand.Next(1000, 9999);
            postData += "&timestamp=" + timestamp;
            string signature = Hash(postData, auth_secret);
            postData += "&signature=" + signature;
            byte[] data = encoding.GetBytes(postData);
    
            httpWReq.Method = "POST";
    
            httpWReq.ContentLength = data.Length;
            httpWReq.Headers["QuickBlox-REST-API-Version"] = "0.1.0";
    
            using (Stream stream = httpWReq.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
    
            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(responseString);
    
            var nodes = xmlDoc.SelectNodes("session");
            string token = nodes[0].SelectSingleNode("token").InnerText;
    
            if (HttpContext.Current != null)
                    HttpContext.Current.Cache.Add("QuickBloxToken", token, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 70, 0), System.Web.Caching.CacheItemPriority.Default, null);
                return token;
            }
            else
                return Convert.ToString(HttpContext.Current.Cache["QuickBloxToken"]);
    
        }
    

    【讨论】:

      【解决方案2】:

      是的,当您在服务器响应中检索此 http 状态时,您还将检索带有错误消息的 response.body,该错误消息描述了此响应的原因。你能检查他吗?

      【讨论】:

      • 我遇到了完全相同的问题,并且正文显示“意外签名”。我正在生成与 HMAC-SHA-1 部分中提到的 here 完全相同的签名。
      • 我已经修好了,唯一的问题是在生成签名时,参数列表没有按字母顺序排序。
      【解决方案3】:

      //使用Restshrap

       public static string token()
          {
              Applicationdetail obj = new Applicationdetail();
              string application_id = obj.application_id.ToString();
              string auth_key = obj.auth_key;
              string timestamp = Timestamp();
              string auth_secret = obj.secretkey;
      
              string postData = "application_id=" + application_id;
              postData += "&auth_key=" + auth_key;
              Random rand = new Random();
              postData += "&nonce=" + rand.Next(1000, 9999);
              postData += "&timestamp=" + timestamp;
              string signature = Hash(postData, auth_secret);
              postData += "&signature=" + signature;
              RestSharp.RestClient client = new                  RestSharp.RestClient("https://api.quickblox.com/session.json?"+ postData);
              RestSharp.RestRequest request = new RestSharp.RestRequest(RestSharp.Method.POST);
              request.AddHeader("QuickBlox-REST-API-Version", " 0.1.0");        
              var result = client.Execute(request);
              if (result != null && !string.IsNullOrEmpty(result.Content))
              {
                  sessionRootObject tokenobj = 
                   JsonConvert.DeserializeObject<sessionRootObject>
                   (result.Content);
                  return tokenobj.session.token;
              }
              else
              {
                  return "";
              }
      
          }    
      

      // 添加类

       public class Session
          {
          public int application_id { get; set; }
          public DateTime created_at { get; set; }
          public int id { get; set; }
          public int nonce { get; set; }
          public string token { get; set; }
          public int ts { get; set; }
          public DateTime updated_at { get; set; }
          public int user_id { get; set; }
          public string _id { get; set; }
      }
      public class sessionRootObject
      {
          public Session session { get; set; }
      }
      
        public class Applicationdetail
         {
          public int application_id { get {
                  return add appid;
              } }
          public string auth_key { get { return "enter auth key"; } }
          public string secretkey { get { return "enter secretkey"; } }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多