您的位置:首页 > 其它

QQ登陆,获取QQ用户信息

2015-05-09 15:32 836 查看
一.登陆QQ互联,创建网站应用or移动应用

地址:http://connect.qq.com/

1.获取appid和app key

2.设置网站地址和回调地址,注:回调地址是用户在网页中登陆QQ后,导航到本网站来处理登陆逻辑的地址

3.可以在不审核通过的时候,设定多个协作者来测试登陆

二.参考OAuth2.0开发文档,分四步获取用户信息

地址:点击打开链接

第一步 根据appid 获取 Authorization Code
第二步 根据Authorization Code 获取Access Token (忽略,自动续期refresh_token)
第三步 根据access_token 获取 OpenID
第四步 根据OpenID 调用OpenAPI 获取用户信息

重要参数说明:

state:client端的状态值。用于第三方应用防止CSRF攻击,成功授权后回调时会原样带回。请务必严格按照流程检查用户与state参数状态的绑定。

access_token:access_token有3个月有效期。

openid:用户的ID,与QQ号码一一对应。

openid是此网站上唯一对应用户身份的标识,网站可将此ID进行存储便于用户下次登录时辨识其身份,或将其与用户在网站上的原有账号进行绑定。

注:

1.调用get_user_info接口 参数、返回值说明地址:点击打开链接

2.获取用户的Access Token和OpenID,之后通过调用OpenAPI进行获取用户信息,并进行分享、发表微博或日志、上传图片等更多操作。

其他可用API地址:点击打开链接

三、WebForm代码示例:

1.回调地址处理

/// <summary>
/// QQLoginashx 的摘要说明
/// </summary>
public class QQLoginashx : IHttpHandler, IRequiresSessionState
{

RequestHelper _Request = new RequestHelper();
LogHelper.LogHelper _log = new LogHelper.LogHelper();

private string appid = "101201650";
private string appkey = "3aedba9180bf1bc2c88779476c57e7b2";

public void ProcessRequest(HttpContext context)
{
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
HttpServerUtility server = context.Server;
HttpSessionState session = context.Session;

string thisUrl = req.Url.AbsoluteUri;
string code = _Request.GetStringQuery("code");
//第一次访问
if (code == "")
{
#region 第一步 根据appid 获取 Authorization Code
StringBuilder builder = new StringBuilder(500);
builder.Append("https://graph.qq.com/oauth2.0/authorize?");
builder.AppendFormat("response_type={0}", "code");
builder.AppendFormat("&client_id={0}", appid);
builder.AppendFormat("&redirect_uri={0}", server.UrlDecode(thisUrl));
builder.AppendFormat("&state={0}", "weilanliuxue");//用于自身验证
builder.AppendFormat("&scope={0}", "do_like");
//builder.AppendFormat("&display={0}", "mobile"); moblie浏览器使用

resp.Redirect(builder.ToString(),false);
return;
#endregion
}
string state = _Request.GetStringQuery("state");
//第二次访问
if (code != "" && state == "weilanliuxue")
{
#region 第二步 根据Authorization Code 获取Access Token
StringBuilder builder = new StringBuilder(500);
builder.Append("https://graph.qq.com/oauth2.0/token?");
builder.AppendFormat("grant_type={0}", "authorization_code");
builder.AppendFormat("&client_id={0}", appid);
builder.AppendFormat("&client_secret={0}", appkey);
builder.AppendFormat("&code={0}", code);
builder.AppendFormat("&redirect_uri={0}", server.UrlDecode(thisUrl));
//1.获取并解析 access_token 例如:access_token=***&expires_in=***&refresh_token=***
string result = NetHelper.Get(builder.ToString());
string access_token = "";
try
{
access_token = result.Split('&').FirstOrDefault().Split('=')[1];
}
catch (Exception ex)
{
throw new Exception("解析 access_token 是出错:" + ex.Message);
}
#endregion

#region 第三步 根据access_token 获取 OpenID
string getOpenIDUrl = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
//1.解析返回结果 中的openid
result = NetHelper.Get(getOpenIDUrl);
List<string> resultList = RegexHelper.Matchs(result, "(?<=\")\\w+(?=\")");
string openid = resultList.Last();
#endregion

#region 第四步 根据OpenID 调用OpenAPI 获取用户信息
//第四步 获取用户信息
builder.Clear();
builder.Append("https://graph.qq.com/user/get_user_info?");
builder.AppendFormat("access_token={0}", access_token);
builder.AppendFormat("&oauth_consumer_key={0}", appid);
builder.AppendFormat("&openid={0}", openid);
string userinfo = NetHelper.Get(builder.ToString());
#endregion

try
{
QQInfo info = userinfo.JsonDeserialezer<QQInfo>();
if (info == null)
{
throw new Exception("内部错误:反序列化获取QQInfo对象时失败!");
}
if (info.ret == "0")
{
//获取数据成功,进行其他逻辑处理
_log.WriteLine(userinfo);
//保存session
session["openid"] = openid;
session["qqinfo"] = info;

//返回数据或跳转
resp.Redirect("~/view/QQInfoShow.aspx", false);
}
else
{
throw new Exception(info.msg);
}
}
catch (Exception ex)
{
//获取用户对象出错
//{"ret":-1,"msg":"client request's parameters are invalid, invalid openid"},可能原因:参数为空
string msg = "获取用户信息失败,当时的qqinfo:" + userinfo + "---------出错原因:" + ex.Message;
_log.WriteLine(msg);
}
}
}
public bool IsReusable
{
get
{
return true;
}
}
}


2.可获取QQ信息对象

/// <summary>
/// QQ 用户的信息对象
/// </summary>
public class QQInfo
{
//返回码
public string ret { get; set; }
//如果ret<0,会有相应的错误信息提示,返回数据全部用UTF-8编码。
public string msg { get; set; }
//用户在QQ空间的昵称
public string nickname { get; set; }
//大小为30×30像素的QQ空间头像URL。
public string figureurl { get; set; }
//大小为50×50像素的QQ空间头像URL。
public string figureurl_1 { get; set; }
//大小为100×100像素的QQ空间头像URL。
public string figureurl_2 { get; set; }
//大小为40×40像素的QQ头像URL。
public string figureurl_qq_1 { get; set; }
//大小为100×100像素的QQ头像URL。需要注意,不是所有的用户都拥有QQ的100x100的头像,但40x40像素则是一定会有。
public string figureurl_qq_2 { get; set; }
//性别。 如果获取不到则默认返回"男"
public string gender { get; set; }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: