网站有自己的账号系统,这里使用的第三方登录仅仅是获取第三方账号的唯一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文件里面
vinson