示例#1
0
        /// <summary>
        /// 获取公众账号信息。
        /// </summary>
        /// <param name="authorizerAppId">授权者AppId。</param>
        /// <returns>公众账号信息。</returns>
        public PublicAccountInfo GetPublicAccountInfo(string authorizerAppId)
        {
            Func <string, string> getContent = accessToken => WeiXinHttpHelper.PostString(
                "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" +
                accessToken, new
            {
                component_appid  = _accountModel.AppId,
                authorizer_appid = authorizerAppId
            });

            string content;

            try
            {
                content = getContent(GetAccessToken().AccessToken);
            }
            catch (WeiXinException exception)
            {
                if (exception.ErrorCode == 40001 ||
                    exception.Message == "invalid credential, access_token is invalid or not latest")
                {
                    content = getContent(GetAccessToken(true).AccessToken);
                }
                else
                {
                    throw;
                }
            }

            var obj               = JObject.Parse(content);
            var authorizerInfo    = obj["authorizer_info"];
            var authorizationInfo = obj["authorization_info"];
            var model             = new PublicAccountInfo
            {
                Authorizer =
                    JsonConvert.DeserializeObject <PublicAccountInfo.AuthorizerInfo>(authorizerInfo.ToString()),
                Authorization =
                    JsonConvert.DeserializeObject <PublicAccountInfo.AuthorizationInfo>(authorizationInfo.ToString()),
            };

            model.Authorization.Rights   = GetRights(authorizationInfo);
            model.Authorizer.ServiceType =
                (PublicAccountInfo.ServiceTypeEnum)
                Enum.Parse(typeof(PublicAccountInfo.ServiceTypeEnum),
                           authorizerInfo.SelectToken("service_type_info.id").Value <string>());
            model.Authorizer.AuthenticateType =
                (PublicAccountInfo.AuthenticateTypeEnum)
                Enum.Parse(typeof(PublicAccountInfo.AuthenticateTypeEnum),
                           authorizerInfo.SelectToken("verify_type_info.id").Value <string>());

            return(model);
        }
示例#2
0
        /// <summary>
        /// 获取公众账号信息。
        /// </summary>
        /// <param name="authorizerAppId">授权者AppId。</param>
        /// <returns>公众账号信息。</returns>
        public PublicAccountInfo GetPublicAccountInfo(string authorizerAppId)
        {
            Func<string, string> getContent = accessToken => WeiXinHttpHelper.PostString(
                "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" +
                accessToken, new
                {
                    component_appid = _accountModel.AppId,
                    authorizer_appid = authorizerAppId
                });

            string content;

            try
            {
                content = getContent(GetAccessToken().AccessToken);
            }
            catch (WeiXinException exception)
            {
                if (exception.ErrorCode == 40001 ||
                    exception.Message == "invalid credential, access_token is invalid or not latest")
                {
                    content = getContent(GetAccessToken(true).AccessToken);
                }
                else
                {
                    throw;
                }
            }

            var obj = JObject.Parse(content);
            var authorizerInfo = obj["authorizer_info"];
            var authorizationInfo = obj["authorization_info"];
            var model = new PublicAccountInfo
            {
                Authorizer =
                    JsonConvert.DeserializeObject<PublicAccountInfo.AuthorizerInfo>(authorizerInfo.ToString()),
                Authorization =
                    JsonConvert.DeserializeObject<PublicAccountInfo.AuthorizationInfo>(authorizationInfo.ToString()),
            };

            model.Authorization.Rights = GetRights(authorizationInfo);
            model.Authorizer.ServiceType =
                (PublicAccountInfo.ServiceTypeEnum)
                    Enum.Parse(typeof(PublicAccountInfo.ServiceTypeEnum),
                        authorizerInfo.SelectToken("service_type_info.id").Value<string>());
            model.Authorizer.AuthenticateType =
                (PublicAccountInfo.AuthenticateTypeEnum)
                    Enum.Parse(typeof(PublicAccountInfo.AuthenticateTypeEnum),
                        authorizerInfo.SelectToken("verify_type_info.id").Value<string>());

            return model;
        }