Robots、ParseJson请参考《新浪微博Oauth2.0登录(一)》
第一步,申请appKey,这里不多说,自己去看新浪的说明即可
第二步,引导用户跳转至新浪登录页(请求授权)
参考http://open.weibo.com/wiki/Oauth2/authorize的说明
string apiKey = "App Key";//申请的App Key
string apiKeySecret = "App Secret";//申请的App Secret
#region GotoSinaLogin
public void GotoSinaLogin()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("https://api.weibo.com/oauth2/authorize");
sb.AppendFormat("?client_id={0}", apiKey);
sb.AppendFormat("&redirect_uri={0}","http://www.abc.com/Account/SinaCallBack");//设置用户授权后跳回的页面
Response.Redirect(sb.ToString());
}
#endregion
第三步,用户同意授权后重定向
这一步由新浪完成,用户授权后,将跳转到第二步设置的地址,并在url中附带参数code
http://www.abc.com/Account/SinaCallBack?code=code
第四步,获取授权(根据第三步得到的code获取授权码access_token)
参考http://open.weibo.com/wiki/OAuth2/access_token
获取用户信息参考http://open.weibo.com/wiki/2/users/show
#region 获取用户信息
public void GetSinaUser(string access_token, string uid, string returnUrl)
{
try
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("https://api.weibo.com/2/users/show.json");
sb.AppendFormat("?source={0}", apiKey);
sb.AppendFormat("&access_token={0}", access_token);
sb.AppendFormat("&uid={0}", uid);
Robots _robots = new Robots();
string html = _robots.GetHtml(sb.ToString(), "utf-8");
#region 抓取错误
if (_robots.IsError)
{
Response.Write("<a href='" + returnUrl + "' style='color:#ff6600'>返回来源页</a><hr/><br/>");
Response.Write(sb.ToString() + "<br/><br/>");
string[] _tem = _robots.ErrorMsg.Split("\n".ToCharArray());
if (_tem.Length > 0)
Response.Write(_tem[0]);
Response.End();
}
#endregion
ParseJson parse = new ParseJson();
SortedDictionary<string, string> list = parse.Parse(html);
string sinaId = parse.GetJsonValue(list, "id");//新浪返回的用户id
string userName = parse.GetJsonValue(list, "name");//新浪返回的用户名
string location = parse.GetJsonValue(list, "location");
//
//设置网站登录信息
//
}
catch (Exception e)
{
Response.Write("无法正确获取远程服务器返回的结果<hr/><br/>");
string[] errorMsg = e.ToString().Split("\n".ToCharArray());
if (errorMsg.Length > 0)
Response.Write(errorMsg[0]);
Response.End();
}
}
#endregion
#region SinaCallBack
public void SinaCallBack()
{
string code = Request["code"]==null?"":Request["code"];
string returnUrl = Session["unionReturnUrl"] == null ? "http://www.abc.com/" : Session["unionReturnUrl"].ToString();
try
{
if (!string.IsNullOrEmpty(code))
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("client_id={0}", apiKey);
sb.AppendFormat("&client_secret={0}", apiKeySecret);
sb.AppendFormat("&grant_type=authorization_code");
sb.AppendFormat("&code={0}", code);
sb.AppendFormat("&redirect_uri={0}", returnUrl);
Robots _robots = new Robots();
string html = _robots.Post("https://api.weibo.com/oauth2/access_token", sb.ToString(), "utf-8");
#region 抓取错误
if (_robots.IsError)
{
Response.Write("<a href='http://www.abc.com/' style='color:#ff6600'>返回网站首页</a>");
Response.Write(" <a href='" + returnUrl + "' style='color:#ff6600'>返回来源页</a><hr/><br/>");
Response.Write(sb.ToString() + "<br/><br/>");
string[] _tem = _robots.ErrorMsg.Split("\n".ToCharArray());
if (_tem.Length > 0)
Response.Write(_tem[0]);
Response.End();
}
#endregion
ParseJson parse = new ParseJson();
SortedDictionary<string, string> list = parse.Parse(html);
string access_token = parse.GetJsonValue(list, "access_token");//授权码
string userid = parse.GetJsonValue(list, "uid");//用户ID(数字)
this.GetSinaUser(access_token, userid, returnUrl);
}
}
catch (Exception e)
{
Response.Write("无法正确获取远程服务器返回的结果<hr/><br/>");
string[] errorMsg = e.ToString().Split("\n".ToCharArray());
if (errorMsg.Length > 0)
Response.Write(errorMsg[0]);
Response.End();
}
Response.Write("无法正确获取远程服务器返回的结果Code=" + code);
Response.End();
}
#endregion