/// <summary> /// 授权获取authorize_code. /// </summary> private async void GetAuthorizeCode() { string oauthUrl = string.Format("{0}/oauth2/authorize?client_id={1}&response_type=code&redirect_uri={2}&display=mobile" , Constants.ServerUrl2_0, SdkData.AppKey, SdkData.RedirectUri); Uri startUri = new Uri(oauthUrl, UriKind.Absolute); Uri endUri = new Uri(SdkData.RedirectUri, UriKind.Absolute); // 调出认证页面. var authenResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri, endUri); switch (authenResult.ResponseStatus) { case WebAuthenticationStatus.Success: { string authorize_code = string.Empty; var data = authenResult.ResponseData; authorize_code = SdkUility.GetQueryParameter(data, "code"); if (string.IsNullOrEmpty(authorize_code) == false) { Authorize(authorize_code); } } break; case WebAuthenticationStatus.UserCancel: { SdkAuthError err = new SdkAuthError(); err.errCode = SdkErrCode.USER_CANCEL; err.errMessage = Constants.SdkMsg.OAuthUserCanceled; if (null != LoginCallback) { LoginCallback(false, err, null); } } break; case WebAuthenticationStatus.ErrorHttp: default: { SdkAuthError err = new SdkAuthError(); err.errCode = SdkErrCode.NET_UNUSUAL; err.errMessage = Constants.SdkMsg.NetException; if (null != LoginCallback) { LoginCallback(false, err, null); } } break; } }
/// <summary> /// 处理授权结果. /// </summary> /// <param name="response"></param> private void HandleResult(RestResponse response) { SdkAuthError err = new SdkAuthError(); string responseStr = response.Content; if (response.StatusCode != (int)HttpStatusCode.OK || response.ErrorException != null) { if (response.ContentLength == 0D) { err.errCode = SdkErrCode.NET_UNUSUAL; if (null != LoginCallback) { LoginCallback(false, err, null); } return; } // 解析错误信息. OAuthErrRes errRes = SerializeOAuthResult <OAuthErrRes>(responseStr); err.errCode = SdkErrCode.SERVER_ERR; err.specificCode = errRes.ErrorCode; err.errMessage = errRes.errDes; if (null != LoginCallback) { LoginCallback(false, err, null); } } else { err.errCode = SdkErrCode.SUCCESS; SdkAuth2Res oauthResult = SerializeOAuthResult <SdkAuth2Res>(responseStr); // 保存AccessToken. SdkData.AccessToken = oauthResult.AccessToken; localSettings.Values[Constants.ACCESSTOKEN] = oauthResult.AccessToken; localSettings.Values[Constants.EXPIREDTIME] = string.IsNullOrEmpty(oauthResult.ExpriesIn) ? 0: long.Parse(oauthResult.ExpriesIn); localSettings.Values[Constants.LASTAUTHTIME] = Unix2DateTime.GetUnixTimestamp(); if (oauthResult.RefreshToken != null) { localSettings.Values[Constants.REFRESHTOKEN] = oauthResult.RefreshToken; } if (null != LoginCallback) { LoginCallback(true, err, oauthResult); } } }
/// <summary> /// 授权登陆. /// </summary> public void BeginOAuth() { // 必要参数检查. if (string.IsNullOrEmpty(SdkData.AppKey) || string.IsNullOrEmpty(SdkData.AppSecret) || string.IsNullOrEmpty(SdkData.RedirectUri)) { SdkAuthError err = new SdkAuthError(); err.errCode = SdkErrCode.XPARAM_ERR; err.errMessage = Constants.SdkMsg.MissParam; if (null != LoginCallback) { LoginCallback(false, err, null); } return; } GetAuthorizeCode(); }