http://www.pp6.cn/Index.aspxjavascript
http://www.pp6.cn/Login.aspxjava
網站有本身的帳號系統,這裏使用的第三方登陸僅僅是獲取第三方帳號的惟一id,暱稱,性別進行本網站的自動註冊。web
自行到QQ,人人網和新浪微博的開發平臺申請並得到對應的appajax
qq:json
sina:api
人人:cookie
在作人人網的鏈接時審覈了3次才經過:app
引入qq的jsdom
<script type="text/javascript" src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js" data-appid="你申請的id" charset="utf-8"></script> <script type="text/javascript" src="http://qzonestyle.gtimg.cn/qzone/openapi/qc_loader.js" charset="utf-8" data-callback="true"></script>
不須要按照官網的作法,登陸的作法也不過是跳轉的一個連接而已,改成本身相應的參數就能夠:async
<tr> <td align="right" style="font-size:12px;">其餘方式:</td> <td class="loginap"> <% string href1 = "http://openapi.qzone.qq.com/oauth/show?which=ConfirmPage&display=pc&client_id=你申請的id&redirect_uri=http://www.pp6.cn/Login.aspx?type=qq_true_" + url + "&response_type=token&scope=all"; string href2 = "http://graph.renren.com/oauth/grant?client_id=你申請的id&redirect_uri=http://www.pp6.cn/Login.aspx?type=rr_true_" + url.Replace("?","%3F") + "&response_type=code&display=page&secure=true&origin=00000"; string href3 = "https://api.weibo.com/oauth2/authorize?client_id=你申請的id&redirect_uri=http://www.pp6.cn/Login.aspx?type=sina_true_" + url + "&response_type=code&state=2"; %> <a href="<%=href1 %>" class="qqicon" title="QQ登陸"></a> <a href="<%=href2 %>" class="rricon" title="人人登陸"></a> <a href="<%=href3 %>" class="sinaicon" title="新浪登陸"></a> </td> </tr>
獲取信息並註冊:
<script type="text/javascript"> var code = "<%=code %>"; var error = "<%=error %>"; var type = "<%=type %>";
if (type == "qq") { //從頁面收集OpenAPI必要的參數。get_user_info不須要輸入參數,所以paras中沒有參數 var paras = {}; //用JS SDK調用OpenAPI QC.api("get_user_info", paras) //指定接口訪問成功的接收函數,s爲成功返回Response對象 .success(function (s) { //成功回調,經過s.data獲取OpenAPI的返回數據 QC.Login.getMe(function (openId, accessToken) { qq_Login(s, openId, accessToken); }) }).complete(function (c) { if (c.ret == 0) { if (QC.Login.check()) { } } }); } if (code != "" && error != "error") { if (type == "rr" || type == "sina") { var id = "<%=id %>"; var name = "<%=name %>"; var imageurl = "<%=imageurl %>"; var sex = "<%=sex %>"; rr_Login(id, name, sex, imageurl); } } function qq_Login(s, openId, accessToken) { $(function () { $.ajax({ contentType: "application/json", url: "/WS/vinson.asmx/LoginQQ", data: "{OpenID:'" + openId + "',NickName:'" + s.data.nickname + "',gender:'" + s.data.gender + "',figureurl_qq_1:'" + s.data.figureurl_qq_1 + "',password:'" + randomPassword(8) + "'}", type: "POST", dataType: "json", async: false, // 設爲同步請求,執行完此項後再繼續執行後面的語句 success: function (json) { json = eval("(" + json.d + ")"); $("#qqLoginBtn").hide(); QC.Login.signOut(); //立刻註銷qq的登陸,改而使用本網站的帳號系統 window.parent.location.href = "<%=url %>"; }, error: function (err, ex) { alert(err.responseText); } }); }); } function rr_Login(id, name, sex, imageurl) { $(function () { $.ajax({ contentType: "application/json", url: "/WS/vinson.asmx/LoginQQ", data: "{OpenID:'" + id + "',NickName:'" + name + "',gender:'" + sex + "',figureurl_qq_1:'" + imageurl + "',password:'" + randomPassword(8) + "'}", type: "POST", dataType: "json", async: false, // 設爲同步請求,執行完此項後再繼續執行後面的語句 success: function (json) { json = eval("(" + json.d + ")"); window.parent.location.href = "<%=url %>"; }, error: function (err, ex) { alert(err.responseText); } }); }); } function delCookie(name){ //爲了刪除指定名稱的cookie,能夠將其過時時間設定爲一個過去的時間 var date = new Date(); date.setTime(date.getTime() - 10000); document.cookie = name + "=a; expires=" + date.toGMTString(); }
</script>
密碼就隨機生成好了:
function randomPassword(length, special) { var iteration = 0; var password = ""; var randomNumber; if (special == undefined) { var special = false; } while (iteration < length) { randomNumber = (Math.floor((Math.random() * 100)) % 94) + 33; if (!special) { if ((randomNumber >= 33) && (randomNumber <= 47)) { continue; } if ((randomNumber >= 58) && (randomNumber <= 64)) { continue; } if ((randomNumber >= 91) && (randomNumber <= 96)) { continue; } if ((randomNumber >= 123) && (randomNumber <= 126)) { continue; } } iteration++; password += String.fromCharCode(randomNumber); } return password; }
人人網和新浪微博的登陸信息獲取,不像qq那樣直接使用js獲取,而是使用第三方的sdk獲取的:
protected string url = GameRequest.GetQueryString("url"); protected string isShow = GameRequest.GetQueryString("isShow"); protected string type = GameRequest.GetQueryString("type"); protected string code = GameRequest.GetQueryString("code"); protected string access_token = GameRequest.GetQueryString("#access_token"); protected string id = ""; protected string name = ""; protected string imageurl = ""; protected string sex = ""; protected string error = ""; protected void Page_Load(object sender, EventArgs e) { Themes.Standard.Common_Header sHeader = (Themes.Standard.Common_Header)this.FindControl("sHeader"); sHeader.title = "會員中心"; if (url == "" || url==null) url = "/Member/MIndex.aspx"; if (isShow == "" || isShow == null) isShow = "false"; if (code == "" || code == null) code = ""; if (access_token == "" || access_token == null) access_token = ""; string[] values = type.Split('_'); if (values.Length == 3) { type = values[0]; isShow = values[1]; url = values[2]; } if (code != "") { if (type == "rr") { getInfoFromRenr(); } if (type == "sina") { getInfoFromSina(); } } else { isShow = "false"; } } /// <summary> /// 獲取人人網登陸的用戶信息 /// </summary> private void getInfoFromRenr() { try { APIValidation av = new APIValidation(); List<APIParameter> parameters = new List<APIParameter>() { new APIParameter("userId", av.GetUserId()) }; RennClient rr = new RennClient(); string responseData = rr.Request("GET", "/v2/user/get", parameters); RennUserResponse resp = new JavaScriptSerializer().Deserialize<RennUserResponse>(responseData); id = resp.response.id; name = resp.response.name; imageurl = resp.response.avatar[0].url; sex = resp.response.basicInformation.sex == "MALE" ? "男" : "女"; } catch { error = "error"; isShow = "false"; Show("未審覈的應用使用人數超過限制,拍拍樂的人人網鏈接正在測試中"); } } /// <summary> /// 獲取新浪微博登陸的用戶信息 /// </summary> private void getInfoFromSina() { try { string AppKey = "你申請的AppKey"; string AppSecret = "你申請的AppSecret"; string CallbackUrl = "http://www.pp6.cn"; var oauth = new OAuth(AppKey, AppSecret, CallbackUrl); /* * 正常的流程或Web流程: * 1. 獲取受權地址 * 2. 訪問受權地址 * 3. 受權成功後自動跳轉至callback指定的網站,並得到code * 4. 經過code換取access token */ if (!string.IsNullOrEmpty(code)) { var accessToken = oauth.GetAccessTokenByAuthorizationCode(code).Token;//注意:callback指定的url必須一致 oauth = new OAuth(AppKey, AppSecret, accessToken, ""); var sina = new Client(oauth); var userInfo = sina.API.Users.Show(sina.API.Account.GetUID()); id = userInfo.ID; name = userInfo.Name; imageurl = userInfo.ProfileImageUrl; sex = userInfo.Gender == "MALE" ? "男" : "女"; } } catch { error = "error"; isShow = "false"; Show("未審覈的應用使用人數超過限制,拍拍樂的新浪網鏈接正在測試中"); } }
回調頁有參數怎麼辦?連續多個參數可能沒法獲取,因此這裏把參數合併到一個參數裏面:
"http://www.pp6.cn/Login.aspx?type=sina_true_" + url;
再在後臺把他們分隔開來:
string[] values = type.Split('_'); if (values.Length == 3) { type = values[0]; isShow = values[1]; url = values[2]; }
這裏的參數type表示是使用何種方式登陸,isShow指示是否彈出遮罩層,url纔是咱們真正想跳轉的連接,至於redirect_uri是咱們獲取信息的代碼功能頁面:
redirect_uri=http://www.pp6.cn/Login.aspx
能夠把AppKey寫到web.config文件裏面