連接QQ:tencent://message/?uin=295611875&Site=hexun.com/ngdao&Menu=yesjson
這兩天在作網站第三方登陸,總結一下QQ登陸吧,支付寶就不用了(下載dome把ID什麼的換一換就基本能夠了。),本文主要說的是代碼的實現方式,邏輯部分主要仍是根據幫助文檔來的。不懂的同窗能夠先看看文檔。 http://open.qq.com/app
直接上代碼吧:dom
1 string client_id = "分配的APP ID"; 2 string appid = " 分配的 APP KEY"; 3 string Return_url = "返回處理的url"; 4 public string Authorize() 5 { 6 string state = new Random(100000).Next(99, 99999).ToString();//隨機數 7 HttpHelper http = new HttpHelper(); 8 string url = string.Format("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={0}&redirect_uri={1}&state={2}", client_id, Return_url, state); 9 string str="<script> location.href='" + url + "'</script>"; 10 return str; 11 12 }
點擊事件直接調用Authorize()就能夠了,我是直接用跳轉連接處理的。網站
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 QQ_Login q = new QQ_Login(); 4 string url = q.Authorize();//這裏調用 5 Response.Write(url); 6 }
效果:ui
點擊圖像,接着在,回調的url裏面調用下面的方法,參數再往下看一點;url
1 public User_info Back_User(string code) 2 { 3 string state = new Random(100000).Next(99, 99999).ToString();//隨機數 4 User_info ui = new User_info(); 5 string url = string.Format("https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id={0}&client_secret={1}&code={2}&redirect_uri={3}&state={4}", client_id, appid, code, Return_url, state); 6 string uu = HttpHelper.HtmlFromUrlGet(url);//處理http請求幫助類 7 string code1 = uu.Split('&')[0].Split('=')[1].ToString();//得到access_token 8 9 //根基access_token獲取用戶惟一OpenID 10 string url_me = string.Format("https://graph.qq.com/oauth2.0/me?access_token={0}" , code1); 11 string callback = HttpHelper.HtmlFromUrlGet(url_me);//這裏獲取的 12 callback = callback.Substring(callback.IndexOf('(') + 1, (callback.IndexOf(')') - callback.IndexOf('(') - 1)).Trim(); 13 // dynamic jsonP = JValue.Parse(callback); //這裏我用的是一個序列化類庫 Newtonsoft.Json.dll 6.0.4版本無奈公司版本太低 只能用4.0的版本 建議使用6.0.4版本 14 a jsonP = JsonConvert.DeserializeObject<a>(callback);//Newtonsoft.Json.dll 4.0或4.5版本 15 string OpenID = jsonP.openid;//獲取用戶惟一的OpenID 16 17 //根據OpenID獲取用戶信息 能夠顯示更多 用的就幾個 須要的能夠本身在下面加 18 string getinfo = string.Format("https://graph.qq.com/user/get_user_info?access_token={0}&oauth_consumer_key={1}&openid={2}", code1, client_id, OpenID); 19 string user = HttpHelper.HtmlFromUrlGet(getinfo); 20 b info = JsonConvert.DeserializeObject<b>(user); 21 ui.Name = info.nickname; 22 ui.OpenID = OpenID; 23 ui.img_qq100 = info.figureurl_qq_1; 24 ui.img_qq50 = info.figureurl_qq_2; 25 ui.city = info.city; 26 ui.year = info.year; 27 ui.Type = 1; 28 return ui; 29 30 }
點擊圖像時跳轉到指定的回調地址,並在redirect_uri地址後帶上Authorization Code;spa
1 2 User_info ui = new User_info(); 3 var verifier = Request.Params["code"]; //獲取Authorization Code 4 ui = q.Back_User(verifier); 掉用方法並返回數據
再把返回的類貼出來吧code
1 public class User_info 2 { 3 public string OpenID { get; set; }//用戶惟一appid 4 5 public string Name { get; set; } 6 7 public string img_qq50 { get; set; } //QQ圖像40*40 8 9 public string img_qq100 { get; set; } 10 11 public string city { get; set; } 12 13 public int year { get; set; } 14 }
1 public class a { 2 3 public string client_id { get; set; } 4 5 public string openid { get; set; } 6 }