qq互联只需要备案即可申请,申请成功后可以到qq互联官网查看教程,本站开始想使用js的教程但是由于本站需要绑定本站的账号用js教程无法完成,所以使用原始的oauth2.0来完成。
申请qq互联接口
申请的时候需要注意将申请的应用名称和备案域名的网站名称一致才能通过
开始编码实现
- 展示qq登录的按钮,可以到官网下载登录的logo。
- 将点击按钮后的链接跳到你申请的回掉页面。
- 回掉页面设计一般是绑定已有账号和绑定新账号其实就是登录和注册。
- 回掉页面展示效果(可自行设计)。
- 实现服务端的业务逻辑。
查阅相关案例都说qq登录无须绑定已有账号,这样失去了qq登录的用户体验,但是本人觉得还是有必要的,下次登录的话即可直接登录。
服务端代码
public ActionResult QQReturn()
{
int qqid = 0;
string name = "", image = "",sex="";
#region 首次调用
if (string.IsNullOrEmpty(Request["code"]))
{
#region 获取code
string url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" + appid + "&redirect_uri=" + HttpUtility.UrlEncode("http://pqpqpq.cn/account/qqreturn") + "&state=loginsuccess&scope=get_user_info,get_info";
Response.Redirect(url);
#endregion
}
else
{
#region 获取access_token
if (Request["state"] == "loginsuccess")
{
string code = Request["code"];
string url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" + appid + "&client_secret=" + appkey + "&code=" + code + "&redirect_uri=" + HttpUtility.UrlEncode("http://pqpqpq.cn/account/qqreturn");
string str = RequestType.HttpGet(url);
if (!string.IsNullOrEmpty(str) && str.IndexOf('&') != -1)
{
if (!str.Contains("msg"))
{
string[] arr = str.Split('&');
string[] arr1 = new string[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
arr1[i] = arr[i].Split('=')[1];
}
access_token = arr1[0];
refresh_token = arr1[2];
expires_in = arr1[1];
}
}
}
#endregion
}
#endregion
string url1 = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
string str1 = RequestType.HttpGet(url1);
if (!string.IsNullOrEmpty(str1))
{
str1 = str1.Replace("callback(", "").Replace(")", "").Replace(";", "");
}
//AddLog(str1);
AccessToken openid = JsonConvert.DeserializeObject<AccessToken>(str1);
if (openid != null)
{
var isopenid = db.person_qq_login.Where(c => c.openid == openid.openid).FirstOrDefault();
if (isopenid == null)
{
#region 插入qq登录信息
string geturl = "https://graph.qq.com/user/get_user_info?access_token=" + access_token + "&oauth_consumer_key=" + appid + "&openid=" + openid.openid;
string userstr = RequestType.HttpGet(geturl);
AddLog(userstr);
UserInfo userdata = JsonConvert.DeserializeObject<UserInfo>(userstr);
if (userdata.ret == 0)
{
person_qq_login qq = new person_qq_login();
qq.figureurl = userdata.figureurl;
qq.figureurl_1 = userdata.figureurl_1;
qq.figureurl_2 = userdata.figureurl_2;
qq.figureurl_qq_1 = userdata.figureurl_qq_1;
qq.figureurl_qq_2 = userdata.figureurl_qq_2;
qq.gender = userdata.gender;
qq.is_yellow_vip = userdata.is_yellow_vip;
qq.is_yellow_year_vip = userdata.is_yellow_year_vip;
qq.level = userdata.level;
qq.msg = userdata.msg;
qq.nickname = userdata.nickname;
qq.ret = userdata.ret;
qq.vip = userdata.vip;
qq.yellow_vip_level = userdata.yellow_vip_level;
qq.openid = openid.openid;
qq.addTime = DateTime.Now;
db.person_qq_login.Add(qq);
db.SaveChanges();
if (db.SaveChanges()>0)
{
qqid = qq.ID;
name = userdata.nickname;
image = userdata.figureurl_qq_2;
sex = userdata.gender;
}
}
#endregion
}
else
{
var isuser = db.person_user.Where(c => c.qqID == isopenid.ID).FirstOrDefault();
if (isuser == null)
{
qqid = isopenid.ID;
name = isopenid.nickname;
image = isopenid.figureurl_qq_2;
}
else
{
//记录session
Session["user_model"] = isuser;
return RedirectToAction("Index", "Center");
}
}
}
ViewData["qqid"] = qqid.ToString();
ViewData["nickname"] = name;
ViewData["faceimage"] = image;
ViewData["sex"] = sex;
return View();
}