예제 #1
0
        /// <summary>
        /// 获取已关注用户列表
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        internal static List <string> GetSubscribeUserList(string accessToken)
        {
            var result      = new List <string>();
            var next_openid = string.Empty;
            var api         = "https://api.weixin.qq.com/cgi-bin/user/get";
            var total       = 0;
            var readCount   = 0;

            do
            {
                var url = string.Empty;
                if (string.IsNullOrEmpty(next_openid))
                {
                    url = string.Format("{0}?access_token={1}", api, accessToken);
                }
                else
                {
                    url = string.Format("{0}?access_token={1}&next_openid={2}", api, accessToken, next_openid);
                }
                var json = HttpRequestHelper.GetHttp_ForamtByJson(url);
                Log.Debug("\r\n获取已关注列表 json返回值:>>\r\n{0}", json);
                var returnCode = GlobalReturnCode.GetReturnCode(json);
                if (returnCode.IsRequestSuccess)
                {
                    var jsonObj = JsonSerializerHelper.Deserialize(json);
                    total = Convert.ToInt32(jsonObj["total"]);
                    var count = Convert.ToInt32(jsonObj["count"]);
                    next_openid = Convert.ToString(jsonObj["next_openid"]);
                    readCount  += count;
                    if (count > 0)
                    {
                        var openids = (jsonObj["data"] as Dictionary <string, object>)["openid"];
                        if (openids is ArrayList)
                        {
                            foreach (var item in openids as ArrayList)
                            {
                                result.Add(item.ToString());
                            }
                        }
                        else
                        {
                            Log.Warning("获取已关注列表转换 openid 类型失败,实际类型:" + openids.ToString());
                        }
                    }
                }
                else
                {
                    throw new WeixinRequestApiException(string.Format("获取已关注列表失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
                }
            } while (total > readCount);
            return(result);
        }
예제 #2
0
        private static Dictionary <string, object> GetAccessTokenDic(string appId, string _AppSecret)
        {
            var url  = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appId, _AppSecret);
            var json = HttpRequestHelper.GetHttp_ForamtByJson(url);

            Log.Debug("\r\n获取 access_token json返回值:>>\r\n{0}", json);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取 access_token 失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var jsonObj = JsonSerializerHelper.Deserialize(json);

            return(jsonObj);
        }
예제 #3
0
파일: OAuth2.cs 프로젝트: chenz2008/WeiXin
        internal static OAuth2AccessToken GetOAuthAccessToken(string code, string appId, string appSecret)
        {
            var url  = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appId, appSecret, code);
            var json = HttpRequestHelper.GetHttp_ForamtByJson(url);

            Log.Debug("\r\n获取 OAuth2.0 access_token json返回值:>>\r\n{0}", json);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取 OAuth2.0 access_token 失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var oAuth2AccessToken = JsonSerializerHelper.ConvertJsonStringToObjectByJsonPropertyAttribute <OAuth2AccessToken>(json);

            return(oAuth2AccessToken);
        }
예제 #4
0
파일: OAuth2.cs 프로젝트: chenz2008/WeiXin
        internal static OAuth2UserInfo GetUserInfo(string openId, string oAuthAccessToken)
        {
            var url  = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", oAuthAccessToken, openId);
            var json = HttpRequestHelper.GetHttp_ForamtByJson(url);

            Log.Debug("\r\n获取用户信息 json返回值:>>\r\n{0}", json);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取用户信息失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var userInfo = JsonSerializerHelper.ConvertJsonStringToObjectByJsonPropertyAttribute <OAuth2UserInfo>(json);

            return(userInfo);
        }