Esempio n. 1
0
        /// <summary>
        /// 获取access_token(同时验证APPID)
        /// </summary>
        /// <param name="_AppID">第三方用户唯一凭证</param>
        /// <param name="_AppSecret">第三方用户唯一凭证密钥</param>
        /// <returns>返回菜单ACCESS_TOKEN</returns>
        public static string ResetAccessToken(string _AppID, string _AppSecret)
        {
            Shove._IO.Log log = new _IO.Log("WeixinGongzhong");

            if (String.IsNullOrEmpty(_AppID) || String.IsNullOrEmpty(_AppSecret))
            {
                log.Write("从微信服务器获取 access_token 发生错误:未提供有效的 AppID、AppSecret,请通过 Utility.InitializeAccessToken(AppID, AppSecret) 方法提供参数后再进行接口访问。");

                return("");
            }

            WebClient webClient = new WebClient();

            Byte[] bytes = null;

            try
            {
                bytes = webClient.DownloadData(
                    string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",
                                  _AppID, _AppSecret));
            }
            catch (Exception e)
            {
                log.Write("从微信服务器获取 access_token 发生错误:" + e.Message);

                return("");
            }
            finally
            {
                webClient.Dispose();
            }

            if (bytes == null || bytes.Length == 0)
            {
                log.Write("从微信服务器获取 access_token 发生错误");

                return("");
            }

            string result = System.Text.Encoding.UTF8.GetString(bytes);

            if (result.IndexOf("errcode") >= 0)
            {
                log.Write("从微信服务器获取 access_token 发生错误:" + result);

                return("");
            }

            string[] res = result.Split('\"');

            //获取的access_token
            access_token = res[3].ToString();

            //更新access_token过期时间
            last_token_datetime = DateTime.Now;

            return(access_token);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取用户的基本信息
        /// </summary>
        /// <param name="OpenId">用户账号的唯一标识ID</param>
        /// <param name="lang">返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 为空默认中文简体</param>
        /// <param name="errorDescription">错误描述</param>
        /// <returns>返回用户基本信息实体UserInformation</returns>
        public static UserInformation GetUserInformation(string OpenId, string lang, ref string errorDescription)
        {
            Shove._IO.Log log = new _IO.Log("WeixinGongzhong");

            errorDescription = "";
            lang             = string.IsNullOrEmpty(lang) ? "zh_CN" : lang.Trim();

            if (string.IsNullOrEmpty(OpenId))
            {
                errorDescription = "OpenId不正确,null";
                return(null);
            }

            if (string.IsNullOrEmpty(Utility.Access_token))
            {
                errorDescription = "发生错误:access_token为null,请查看页面的Load事件是否调用了Utility.GetAccessToken()方法";
                return(null);
            }

            string errorCode = "";

            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                try
                {
                    byte[] by = client.DownloadData(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang={2}", Utility.Access_token, OpenId, lang));

                    errorCode = System.Text.Encoding.UTF8.GetString(by);
                }
                catch (Exception ex)
                {
                    errorDescription = ex.Message;
                    return(null);
                }
            }

            if (errorCode.IndexOf("errcode") >= 0)
            {
                errorDescription = ErrorInformation.GetErrorCode(errorCode, "errcode");
                log.Write(errorDescription + "\t" + Utility.Access_token + "\t" + Utility.AppID + "\t" + Utility.AppSecret);
                return(null);
            }

            return(JsonToObject(errorCode, ref errorDescription));
        }