/// <summary>
        /// 尝试从JSON字符串解析AccessToken
        /// </summary>
        /// <param name="json">JSON字符串</param>
        /// <param name="msg">如果解析成功,返回AccessToken;否则,返回null。</param>
        /// <returns>返回是否解析成功</returns>
        internal static bool TryParse(string json, out OAuthAccessToken token)
        {
            bool success = false;

            token = null;
            try
            {
                token   = Parse(json);
                success = true;
            }
            catch { }
            return(success);
        }
        /// <summary>
        /// 刷新access token
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="refreshToken">用户刷新token</param>
        /// <param name="errorMessage">返回获取是否成功</param>
        /// <returns>返回access token;如果获取失败,返回null。</returns>
        public static OAuthAccessToken Refresh(string userName, string refreshToken, out ErrorMessage errorMessage)
        {
            OAuthAccessToken token = null;

            if (string.IsNullOrWhiteSpace(refreshToken))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "用户刷新token不能为空。");
                return(token);
            }
            AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);

            if (account == null)
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取公众号信息失败。");
                return(token);
            }
            string url = string.Format(urlForRefreshingAccessToken, account.AppId, refreshToken);
            string responseContent;

            if (!HttpHelper.Request(url, out responseContent, httpMethod, (string)null))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "从微信服务器获取响应失败。");
            }
            else if (ErrorMessage.IsErrorMessage(responseContent))
            {
                errorMessage = ErrorMessage.Parse(responseContent);
            }
            else
            {
                var result = JsonConvert.DeserializeAnonymousType(responseContent,
                                                                  new { access_token = "", expires_in = 0, refresh_token = "", openid = "", scope = "" });
                token        = new OAuthAccessToken(result.access_token, result.expires_in, result.refresh_token, result.openid, result.scope);
                errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取access token成功。");
            }
            return(token);
        }
示例#3
0
 /// <summary>
 /// 获取OAuth用户基本信息
 /// </summary>
 /// <param name="accessToken">网页access token</param>
 /// <param name="openId">用户id</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回用户基本信息;如果获取失败,返回null。</returns>
 public static UserInfo GetOAuthUserInfo(string accessToken, string openId, out ErrorMessage errorMessage)
 {
     return(OAuthAccessToken.GetUserInfo(accessToken, openId, out errorMessage));
 }