using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using LitJson; namespace HttpProxy { public class QQAuth { public static bool AuthToken(string accountid, string token, string ip, out bool exception) { //成功,打印一下日志 TraceLog.Debug("QQAuth.AuthToken, start accountid {0}", accountid); long timestamp = HttpProxyServerUtils.GetTimeSecond(); string msdkkey = HttpProxyServerUtils.GetServerConfig().msdkkey; string appid = HttpProxyServerUtils.GetServerConfig().qq_appid; string appkey = HttpProxyServerUtils.GetServerConfig().qq_appkey; string sig = MSDKApi.CalcSig(msdkkey, timestamp); string fullUrl = string.Format("{0}?timestamp={1}&appid={2}&sig={3}&openid={4}&encode=2" , MSDKApi.MSDKUrl + MSDKApi.QQAuthUrl , timestamp , appid , sig , accountid); List> contentParams = new List>(); contentParams.Add(new KeyValuePair("appid", appid)); contentParams.Add(new KeyValuePair("openid", accountid)); contentParams.Add(new KeyValuePair("openkey", token)); contentParams.Add(new KeyValuePair("userip", ip)); string strRet = HttpUtils.HttpPost(fullUrl, contentParams, out exception); if (strRet == null) { TraceLog.Error("QQAuth.AuthToken, HttpGet return null query failed"); return false; } LitJson.JsonData jsonData = JsonMapper.ToObject(strRet); LitJson.JsonData ret = jsonData["ret"]; LitJson.JsonData msg = jsonData["msg"]; if (ret == null || msg == null) { TraceLog.Error("QQAuth.AuthToken, ret {0}, no ret or no msg", strRet); return false; } if(ret.ToString() != "0") { TraceLog.Error("QQAuth.AuthToken , msdk server ack failed, ret {0}", strRet); return false; } //成功,打印一下日志 TraceLog.Debug("QQAuth.AuthToken ,ret {0}, accountid {1}", strRet, accountid); return true; } public static string[] QueryUserInfo(string accountid, string token, out bool exception) { //成功,打印一下日志 TraceLog.Debug("QQAuth.QueryUserInfo, start accountid {0}", accountid); long timestamp = HttpProxyServerUtils.GetTimeSecond(); string msdkkey = HttpProxyServerUtils.GetServerConfig().msdkkey; string appid = HttpProxyServerUtils.GetServerConfig().qq_appid; string appkey = HttpProxyServerUtils.GetServerConfig().qq_appkey; string sig = MSDKApi.CalcSig(msdkkey, timestamp); string fullUrl = string.Format("{0}?timestamp={1}&appid={2}&sig={3}&openid={4}&encode=2" , MSDKApi.MSDKUrl + MSDKApi.qqprofile , timestamp , appid , sig , accountid); List> contentParams = new List>(); contentParams.Add(new KeyValuePair("appid", appid)); contentParams.Add(new KeyValuePair("openid", accountid)); contentParams.Add(new KeyValuePair("accessToken", token)); string strRet = HttpUtils.HttpPost(fullUrl, contentParams, out exception); if (strRet == null) { TraceLog.Error("QQAuth.QueryUserInfo, HttpGet return null query failed"); return null; } LitJson.JsonData jsonData = JsonMapper.ToObject(strRet); LitJson.JsonData ret = jsonData["ret"]; LitJson.JsonData msg = jsonData["msg"]; if (ret == null || msg == null) { TraceLog.Error("QQAuth.QueryUserInfo, ret {0}, no ret or no msg", strRet); return null; } if (ret.ToString() != "0") { TraceLog.Error("QQAuth.QueryUserInfo , msdk server ack failed, ret {0}", strRet); return null; } LitJson.JsonData nickName = jsonData["nickName"]; LitJson.JsonData gender = jsonData["gender"]; LitJson.JsonData picture40 = jsonData["picture40"]; if(nickName == null || gender == null || picture40 == null) { TraceLog.Error("QQAuth.QueryUserInfo , nickName gender picture40 failed, ret {0}", strRet); return null; } string[] result = new string[4]; result[0] = nickName.ToString(); result[1] = gender.ToString(); result[2] = picture40.ToString(); //成功,打印一下日志 TraceLog.Debug("QQAuth.QueryUserInfo ,ret {0}, accountid {1}", strRet, accountid); return result; } } }