Example #1
0
        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 = "保存失败。";
            }
        }
Example #2
0
        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&regCallback=" + 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);
                    }
                }
            }
        }