Ejemplo n.º 1
0
        public static RequestApiResult <WeixinThirdPartyGetAuthorizerAccessTokenResult> GetAuthorizerAccessToken(string appId, string refreshToken)
        {
            WeixinThirdPartyGetAuthorizerAccessTokenArgs args = new WeixinThirdPartyGetAuthorizerAccessTokenArgs();

            args.ComponentAppId         = _configuration.ThirdParty.AppId;
            args.AuthorizerAppId        = appId;
            args.AuthorizerRefreshToken = refreshToken;

            string accessToken = ThirdPartyAccessTokenGetter.Get();
            RequestApiResult <WeixinThirdPartyGetAuthorizerAccessTokenResult> result =
                ThirdPartyApi.GetAuthorizerAccessToken(accessToken, args);

            if (result.Success == false)
            {
                if (result.Retry)
                {
                    if (result.ApiError.ErrorCode == 40001)
                    {
                        accessToken = ThirdPartyAccessTokenGetter.Refresh(accessToken);
                    }

                    result = ThirdPartyApi.GetAuthorizerAccessToken(accessToken, args);
                    if (result.Success == false)
                    {
                        _log.Write("ThirdPartyApi.GetAuthorizerAccessToken 失败",
                                   result.GetDetail(), TraceEventType.Warning);
                    }
                }
                else
                {
                    _log.Write("ThirdPartyApi.GetAuthorizerAccessToken 失败",
                               result.GetDetail(), TraceEventType.Warning);
                }
            }
            else
            {
                //保存新的RefreshToken到数据库
                //非常重要,一旦丢失则需要公众号重新授权
                WeixinThirdPartyGetAuthorizerAccessTokenResult token = result.ApiResult;
                ThirdPartyManager.Instance.UpdateAuthorizerRefreshToken(
                    appId, token.AccessToken, DateTime.Now.AddSeconds(token.ExpiresIn), token.RefreshToken);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 使用授权码换取公众号的授权信息
        /// </summary>
        public static RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> GetAuthorizationInfo(string authorizationCode)
        {
            WeixinThirdPartyGetAuthorizationInfoArgs args = new WeixinThirdPartyGetAuthorizationInfoArgs();

            args.ComponentAppId    = _configuration.ThirdParty.AppId;
            args.AuthorizationCode = authorizationCode;

            string accessToken = ThirdPartyAccessTokenGetter.Get();

            RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> result =
                ThirdPartyApi.GetAuthorizationInfo(accessToken, args);

            if (result.Success == false)
            {
                if (result.Retry)
                {
                    if (result.ApiError.ErrorCode == 40001)
                    {
                        accessToken = ThirdPartyAccessTokenGetter.Refresh(accessToken);
                    }

                    result = ThirdPartyApi.GetAuthorizationInfo(accessToken, args);
                    if (result.Success == false)
                    {
                        _log.Write("ThirdPartyApi.GetAuthorizationInfo 失败",
                                   result.GetDetail(), TraceEventType.Warning);
                    }
                }
                else
                {
                    _log.Write("ThirdPartyApi.GetAuthorizationInfo 失败",
                               result.GetDetail(), TraceEventType.Warning);
                }
            }

            //此时数据库中可能还没有建立记录,保存refreshtoken的工作在调用处做

            return(result);
        }