網站有自己的賬號系統,這里使用的第三方登錄僅僅是獲取第三方賬號的唯一id,昵稱,性別進行本網站的自動注冊。
自行到QQ,人人網和新浪微博的開發平台申請並獲得對應的app
qq:
sina:
人人:
在做人人網的連接時審核了3次才通過:
引入qq的js
<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>
不需要按照官網的做法,登錄的做法也不過是跳轉的一個鏈接而已,改為自己相應的參數就可以:
<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文件里面