我正在尝试访问我的QuickBlox应用并使用REST API获取令牌。
我的代码如下所示: http://pastebin.com/rp2KLMp2
请求如下所示(已删除敏感信息): APPLICATION_ID = XXXX&安培; AUTH_KEY = XXXXXXXXXX&安培;随机数= 2851&安培;时间戳= 1389951758&安培;签名= D481F13E87F47D4C17697EF9D2C8E25777E09079
我收到错误: 远程服务器返回错误:(422)Unprocessable Entity
可能是什么问题?
答案 0 :(得分:2)
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 += "×tamp=" + 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"]);
}
答案 1 :(得分:0)
是的,当您在服务器响应中检索此http状态时,您还将检索带有错误消息的response.body,其中描述了此响应的原因。你能检查一下吗?
答案 2 :(得分:0)
//使用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 += "×tamp=" + 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"; } }
}