using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using NetDimension.Weibo; using QConnectSDK; using QConnectSDK.Context; namespace ToothBond.Aiya.Web.Job.Controllers { public class U3LoginController : Controller { Client Sina = null; OAuth oauth = new OAuth( System.Configuration.ConfigurationManager.AppSettings["AppKey"], System.Configuration.ConfigurationManager.AppSettings["AppSecret"], System.Configuration.ConfigurationManager.AppSettings["CallbackURL"]); /// <summary> /// 新浪登陸 /// </summary> /// <returns></returns> public ActionResult SinaLogin() { Sina = new Client(oauth); string url = oauth.GetAuthorizeURL(); return new RedirectResult(url); } /// <summary> /// QQ登陸 /// </summary> /// <returns></returns> public ActionResult QzoneLogin() { var context = new QzoneContext(); string state = Guid.NewGuid().ToString().Replace("-", ""); string scope = "get_user_info"; var authenticationUrl = context.GetAuthorizationUrl(state, scope); HttpContext.Session["requeststate"] = state; return new RedirectResult(authenticationUrl); } /// <summary> /// 新浪回調 /// </summary> /// <returns></returns> public ActionResult SinaCallback() { if (!string.IsNullOrEmpty(Request["code"])) { Sina = new Client(oauth); var token = oauth.GetAccessTokenByAuthorizationCode(Request["code"]); string accessToken = token.Token; string uid = Sina.API.Entity.Account.GetUID(); var dynamic_userInfo = Sina.API.Dynamic.Users.Show(uid); //存cookie TempData["uid"] = uid; TempData["nickName"] = dynamic_userInfo.screen_name; //TempData["from"] = dynamic_userInfo.location; Session["uid"] = uid; Session["nickName"] = dynamic_userInfo.screen_name; return Redirect("/"); } return View(); } /// <summary> /// QQ回調 /// </summary> /// <returns></returns> public ActionResult QzoneCallback() { if (!string.IsNullOrEmpty(Request["code"])) { var verifier = Request.Params["code"]; QOpenClient qzone = null; QConnectSDK.Models.User currentUser = null; string state = Session["requeststate"].ToString(); qzone = new QOpenClient(verifier, state); currentUser = qzone.GetCurrentUser(); string nickName = currentUser.Nickname; string openId = qzone.OAuthToken.OpenId; string accessToken = qzone.OAuthToken.AccessToken; Session["uid"] = openId; Session["nickName"] = nickName; return Redirect("/"); } return View(); } } }新浪微薄 字段說明 - user id: 用戶UID screen_name: 微博暱稱 name: 友好顯示名稱,同微博暱稱 province: 省份編碼(參考省份編碼表) city: 城市編碼(參考城市編碼表) location:地址 description: 我的描述 url: 用戶博客地址 profile_image_url: 自定義圖像 domain: 用戶個性化URL gender: 性別,m--男,f--女,n--未知 followers_count: 粉絲數 friends_count: 關注數 statuses_count: 微博數 favourites_count: 收藏數 created_at: 建立時間 following: 是否已關注(此特性暫不支持) verified: 加V標示,是否微博認證用戶 字段說明 - status created_at: 建立時間 id: 微博ID text: 微博信息內容 source: 微博來源 favorited: 是否已收藏 truncated: 是否被截斷 in_reply_to_status_id: 回覆ID in_reply_to_user_id: 回覆人UID in_reply_to_screen_name: 回覆人暱稱 thumbnail_pic: 縮略圖 bmiddle_pic: 中型圖片 original_pic:原始圖片 user: 做者信息 retweeted_status: 轉發的博文,內容爲status,若是不是轉發,則沒有此字段