private OAuthAPIEntity GetOAuthAPI(int site) { OAuthAPIEntity oauthAPI = new OAuthAPIEntity(); switch (site) { case 0: //新浪微博 oauthAPI.AccessTokenUrl = "http://api.t.sina.com.cn/oauth/access_token"; oauthAPI.AuthorizeUrl = "http://api.t.sina.com.cn/oauth/authorize"; oauthAPI.RequestTokenUrl = "http://api.t.sina.com.cn/oauth/request_token"; break; case 5: //新浪微博2.0 oauthAPI.AccessTokenUrl = "https://api.weibo.com/oauth2/access_token"; oauthAPI.AuthorizeUrl = "https://api.weibo.com/oauth2/authorize"; oauthAPI.RequestTokenUrl = ""; break; case 1: //腾讯微博 oauthAPI.AccessTokenUrl = "https://open.t.qq.com/cgi-bin/access_token"; oauthAPI.AuthorizeUrl = "https://open.t.qq.com/cgi-bin/authorize"; oauthAPI.RequestTokenUrl = "https://open.t.qq.com/cgi-bin/request_token"; break; case 2: //网易微博 oauthAPI.AccessTokenUrl = "http://api.t.163.com/oauth/access_token"; oauthAPI.AuthorizeUrl = "http://api.t.163.com/oauth/authenticate"; oauthAPI.RequestTokenUrl = "http://api.t.163.com/oauth/request_token"; break; case 3: //搜狐微博 oauthAPI.AccessTokenUrl = "http://api.t.sohu.com/oauth/access_token"; oauthAPI.AuthorizeUrl = "http://api.t.sohu.com/oauth/authorize"; oauthAPI.RequestTokenUrl = "http://api.t.sohu.com/oauth/request_token"; break; case 4: //开心网 oauthAPI.AccessTokenUrl = "http://api.kaixin001.com/oauth/access_token"; oauthAPI.AuthorizeUrl = "http://api.kaixin001.com/oauth/authorize"; oauthAPI.RequestTokenUrl = "http://api.kaixin001.com/oauth/request_token"; break; } return(oauthAPI); }
protected void btnSave_Click(object sender, EventArgs e) { OAuthAPIEntity oauthAPI = Session["oauthAPIObj"] as OAuthAPIEntity; try { OAuthAPIDAL.Save(oauthAPI); lblErrorMsg.Text = "保存成功"; } catch (Exception ex) { lblErrorMsg.Text = "保存失败。"; } }
public static OAuthAPIEntity Load(string appKey, string userName, int siteid) { SqlDataReader reader = null; try { string sql = " select id,appkey,appsecret,token,tokensecret,site,username,password from oauthapi where appkey=@appkey and username=@username and site=@siteid"; SqlParameter[] cols = new SqlParameter[3]; cols[0] = new SqlParameter("@appkey", appKey); cols[1] = new SqlParameter("@username", userName); cols[2] = new SqlParameter("@siteid", siteid); reader = SqlHelper.ExecuteReader(Config.TwitterConnectionString, CommandType.Text, sql, cols); OAuthAPIEntity entity = null; if (reader.Read()) { entity = new OAuthAPIEntity(); entity.ID = reader.GetInt32(0); entity.AppKey = reader.GetString(1); entity.AppSecret = reader.GetString(2); entity.Token = reader.GetString(3); entity.TokenSecret = reader.GetString(4); entity.Site = reader.GetInt32(5); entity.UserName = reader.GetString(6); entity.Password = reader.IsDBNull(7) ? string.Empty : reader.GetString(7); } return(entity); } catch (Exception ex) { throw ex; } finally { if (reader != null) { reader.Close(); reader = null; } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["oauthAPIObj"] != null) { OAuthAPIEntity oauthAPI = Session["oauthAPIObj"] as OAuthAPIEntity; drpSite.SelectedIndex = oauthAPI.Site; txtAppKey.Text = oauthAPI.AppKey; txtAppSecret.Text = oauthAPI.AppSecret; txtUserName.Text = oauthAPI.UserName; } if (Request["oauth_verifier"] != null || drpSite.SelectedIndex == 2) { if (!string.IsNullOrEmpty(txtToken.Text)) { return; } var verifier = Request["oauth_verifier"]; IOAuthAPI oauthAPI = Session["oauthAPI"] as IOAuthAPI; if (oauthAPI.GetAccessToken(verifier)) { txtToken.Text = oauthAPI.Token; txtTokenSecret.Text = oauthAPI.TokenSecret; this.lblErrorMsg.Text = "授权成功"; Session["oauthAPI"] = oauthAPI; OAuthAPIEntity oauthAPIEntity = Session["oauthAPIObj"] as OAuthAPIEntity; oauthAPIEntity.Token = oauthAPI.Token; oauthAPIEntity.TokenSecret = oauthAPI.TokenSecret; Session["oauthAPIObj"] = oauthAPIEntity; } } } }
public static void Save(OAuthAPIEntity entity) { try { string sql = "insert into oauthapi(appkey,appsecret,token,tokensecret,site,username,password,version) values(@appkey,@appsecret,@token,@tokensecret,@siteid,@username,@password,@version)"; SqlParameter[] cols = new SqlParameter[8]; cols[0] = new SqlParameter("@appkey", entity.AppKey); cols[1] = new SqlParameter("@appsecret", entity.AppSecret); cols[2] = new SqlParameter("@token", entity.Token); cols[3] = new SqlParameter("@tokensecret", entity.TokenSecret); cols[4] = new SqlParameter("@siteid", entity.Site); cols[5] = new SqlParameter("@username", entity.UserName); cols[6] = new SqlParameter("@password", entity.Password); cols[7] = new SqlParameter("@version", entity.Version); SqlHelper.ExecuteNonQuery(Config.TwitterConnectionString, CommandType.Text, sql, cols); } catch (Exception ex) { throw ex; } }
protected void btnGo_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtAppKey.Text) || string.IsNullOrEmpty(txtAppSecret.Text)) { lblErrorMsg.Text = "请输入appkey和appsecret"; return; } Session.Clear(); var site = int.Parse(drpSite.SelectedValue); if (site == 5) { site = 0; } OAuthAPIEntity entity = OAuthAPIDAL.Load(txtAppKey.Text, txtUserName.Text, site); if (entity != null) { lblErrorMsg.Text = "已经存在"; txtToken.Text = entity.Token; txtTokenSecret.Text = entity.TokenSecret; IOAuthAPI oauthAPI2 = OAuthAPIFactory.CreateOAuthAPI(); oauthAPI2.RequestTokenUrl = entity.RequestTokenUrl; oauthAPI2.AuthorizeUrl = entity.AuthorizeUrl; oauthAPI2.AccessTokenUrl = entity.AccessTokenUrl; oauthAPI2.AppKey = entity.AppKey; oauthAPI2.AppSecret = entity.AppSecret; oauthAPI2.Token = entity.Token; oauthAPI2.TokenSecret = entity.TokenSecret; Session["oauthAPI"] = oauthAPI2; return; } OAuthAPIEntity oauthAPIEntity = GetOAuthAPI(site); oauthAPIEntity.AppKey = txtAppKey.Text; oauthAPIEntity.AppSecret = txtAppSecret.Text; oauthAPIEntity.UserName = txtUserName.Text; oauthAPIEntity.Password = txtPassword.Text; oauthAPIEntity.Site = site; Session["oauthAPIObj"] = oauthAPIEntity; if (int.Parse(drpSite.SelectedValue) >= 5) { IHttpForm http = HttpFormFactory.DefaultHttpForm(); string authorizeFormat = "https://api.weibo.com/oauth2/authorize?client_id={0}&redirect_uri={1}&response_type=code"; string authorize = string.Format(authorizeFormat, oauthAPIEntity.AppKey, "http://barefoot.3322.org/queryservice.svc/query"); HttpFormGetRequest getRequest = new HttpFormGetRequest(); getRequest.Cookies = Login(oauthAPIEntity.UserName, oauthAPIEntity.Password); getRequest.Url = authorize; HttpFormResponse response = http.Get(getRequest); Match m = null; if (!response.Response.StartsWith("\"code=")) { m = Regex.Match(response.Response, "<input\\stype=\"hidden\"\\sname=\"regCallback\"\\svalue=\"(?<regCallback>[^\"]+)\"/>", RegexOptions.IgnoreCase | RegexOptions.Multiline); string regCallback = m.Groups["regCallback"].Value; string regPostData = "action=submit&response_type=code®Callback=" + regCallback + "&redirect_uri=http://barefoot.3322.org/queryservice.svc/query&client_id=" + oauthAPIEntity.AppKey + "&state=&from="; HttpFormPostRawRequest regRequest = new HttpFormPostRawRequest(); regRequest.Data = regPostData; regRequest.Url = "https://api.weibo.com/oauth2/authorize"; regRequest.Cookies = response.Cookies; response = http.Post(regRequest); } string code = response.Response.Trim('\"').Substring(5); HttpFormPostRawRequest request = new HttpFormPostRawRequest(); request.Url = "https://api.weibo.com/oauth2/access_token"; string postDataFormat = "client_id={0}&client_secret={1}&grant_type=authorization_code&code={2}&redirect_uri=http://barefoot.3322.org/queryservice.svc/query"; string postData = string.Format(postDataFormat, oauthAPIEntity.AppKey, oauthAPIEntity.AppSecret, code); request.Data = postData; response = http.Post(request); m = Regex.Match(response.Response, "{\"access_token\":\"(?<token>[^\"]+)\","); string token = m.Groups["token"].Value; txtToken.Text = token; txtTokenSecret.Text = code; this.lblErrorMsg.Text = "授权成功"; oauthAPIEntity.Token = token; oauthAPIEntity.TokenSecret = code; oauthAPIEntity.Version = 2; Session["oauthAPIObj"] = oauthAPIEntity; } else { IOAuthAPI oauthAPI = OAuthAPIFactory.CreateOAuthAPI(); oauthAPI.RequestTokenUrl = oauthAPIEntity.RequestTokenUrl; oauthAPI.AuthorizeUrl = oauthAPIEntity.AuthorizeUrl; oauthAPI.AccessTokenUrl = oauthAPIEntity.AccessTokenUrl; if (oauthAPI.GetRequestToken(oauthAPIEntity.AppKey, oauthAPIEntity.AppSecret, Config.CallbackUrl)) { var authorizationUrl = oauthAPI.GetAuthorize(Config.CallbackUrl); Session["oauthAPI"] = oauthAPI; if (!string.IsNullOrEmpty(authorizationUrl)) { Response.Redirect(authorizationUrl); } } } }