protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (!String.IsNullOrEmpty(Request.QueryString["code"])) { String code = Request.QueryString["code"]; String appKey = Application.Get("OAuthAppKey") as String; String appSecret = Application.Get("OAuthAppSecret") as String; String callbackUrl = Application.Get("OAuthCallbackUrl") as String; OAuth oauth = new OAuth(appKey, appSecret, callbackUrl); AccessToken accessToken = oauth.GetAccessTokenByAuthorizationCode(code); if (accessToken == null) { this.lbMessage.Text = "获取AccessToken,失败,原因未知!"; } if (!String.IsNullOrEmpty(accessToken.ErrorMessage)) { this.lbMessage.Text = "获取AccessToken,失败:" + accessToken.ErrorMessage; } AppContext.AppKey = appKey; AppContext.AppSecret = appSecret; AppContext.CallbackUrl = callbackUrl; AppContext.AccessToken = accessToken; Response.Redirect("~/default.aspx"); } else { } } }
protected void btnAuthon_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(txtAppKey.Text.Trim())) { Utility.MessageBox("App Key 不能为空!"); return; } if (String.IsNullOrEmpty(txtAppSecret.Text.Trim())) { Utility.MessageBox("App Secret 不能为空!"); return; } if (String.IsNullOrEmpty(txtCallbackUrl.Text.Trim())) { Utility.MessageBox("Callback Url 不能为空!"); return; } try { Uri uri = new Uri(txtCallbackUrl.Text.Trim(), UriKind.Absolute); if (!String.Equals(uri.Host, Request.Url.Host)) { Utility.MessageBox("回调地址与当前主域不匹配!"); return; } } catch (UriFormatException ex) { Utility.MessageBox(ex.Message); return; } OAuth oauth = new OAuth(txtAppKey.Text.Trim(), txtAppSecret.Text.Trim(), txtCallbackUrl.Text.Trim()); Application.Set("OAuthAppKey",txtAppKey.Text.Trim()); Application.Set("OAuthAppSecret", txtAppSecret.Text.Trim()); Application.Set("OAuthCallbackUrl", txtCallbackUrl.Text.Trim()); Response.Redirect(oauth.GetAuthorizationUrl()); }
protected void Page_Load(object sender, EventArgs e) { if (Utility.IsAccessTokenValid(AppContext.AccessToken)) { //有效 做你想做的事 lbMessage.Text = AppContext.AccessToken.Token; } else { if (String.IsNullOrEmpty(AppContext.AppKey) || String.IsNullOrEmpty(AppContext.AppSecret) || String.IsNullOrEmpty(AppContext.CallbackUrl)) { Response.Redirect("~/setting.aspx"); } //失效了再获取或者用refreshToken刷新 //refreshToken没有测试过 OAuth oauth = new OAuth(AppContext.AppKey, AppContext.AppSecret, AppContext.CallbackUrl); Response.Redirect(oauth.GetAuthorizationUrl()); } }
private void btnLink_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(txtAppKey.Text.Trim())) { Utility.ShowError("App Key 不能为空!"); return; } if (String.IsNullOrEmpty(txtAppSecret.Text.Trim())) { Utility.ShowError("App Secret 不能为空!"); return; } this.AppKey = txtAppKey.Text.Trim(); this.AppSecret = txtAppSecret.Text.Trim(); String callBackUrl = "urn:ietf:wg:oauth:2.0:oob"; oauth = new OAuth(this.AppKey, this.AppSecret, callBackUrl); oauth.AuthorizeUrl = AppContext.AuthorizeUrl; oauth.AccessTokenUrl = AppContext.AccessTokenUrl; Utility.Run(oauth.GetAuthorizationUrl(), null); Console.WriteLine("打开授权网页!"); }
private void btnOk_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(txtAppKey.Text.Trim())) { Utility.ShowError("App Key 不能为空!"); return; } if (String.IsNullOrEmpty(txtAppSecret.Text.Trim())) { Utility.ShowError("App Secret 不能为空!"); return; } this.AppKey = txtAppKey.Text.Trim(); this.AppSecret = txtAppSecret.Text.Trim(); if (String.IsNullOrEmpty(txtAuthCode.Text.Trim())) { this.AccessToken = null; this.DialogResult = System.Windows.Forms.DialogResult.OK; Console.WriteLine("AccessToken未获取!"); return; } if (oauth == null) { oauth = new OAuth(this.AppKey, this.AppSecret); } AccessToken accessToken = oauth.GetAccessTokenByAccessToken(txtAuthCode.Text.Trim()); if (accessToken == null) { Utility.ShowError("获取AccessToken失败,原因未知!"); return; } if (!String.IsNullOrEmpty(accessToken.ErrorMessage)) { Utility.ShowError("获取AccessToken失败,失败:" + accessToken.ErrorMessage); return; } this.AccessToken = accessToken; Console.WriteLine("获取AccessToken成功!"); this.DialogResult = System.Windows.Forms.DialogResult.OK; }