Exemplo n.º 1
0
        /// <summary>
        /// 第四步:拉取用户信息(需scope为 snsapi_userinfo)
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId">要获取用信息的用户OpenId</param>
        /// <returns></returns>
        public static OAuthUserInfo GetUserInfo(string accessToken, string openId)
        {
            var    url    = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", accessToken, openId);
            string result = RequestUtility.HttpGet(url, null);

            return(ApiHelper.GetResult <OAuthUserInfo>(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 从微信公众平台API获取信息的公共方法
        /// </summary>
        /// <param name="urlFormat">API接口地址格式</param>
        /// <param name="accessToken">微信公众号访问授权AccessToken</param>
        /// <param name="querys">除了AccessToken还需要传递的其他参数</param>
        /// <returns></returns>
        public static T Get <T>(string accessToken, string urlFormat, params string[] querys)
        {
            var    url    = GetApiUrl(urlFormat, accessToken, querys);
            string result = RequestUtility.HttpGet(url, null);

            return(GetResult <T>(result));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取当前菜单,如果菜单不存在,将返回null
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static GetMenuResult GetMenu(string accessToken)
        {
            //微信公众平台获取自定义菜单接口地址
            var           url        = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", accessToken);
            var           jsonString = RequestUtility.HttpGet(url, Encoding.UTF8);
            GetMenuResult finalResult;

            //JavaScriptSerializer js = new JavaScriptSerializer();
            try
            {
                //获取自定义菜单Json结构
                var jsonResult = JsonConvert.DeserializeObject <GetMenuResultJson>(jsonString);
                if (jsonResult.menu == null || jsonResult.menu.button.Count == 0)
                {
                    throw new WeiXinException(jsonResult.errmsg);
                }
                //将Json结构自定义菜单转换为自定义菜单实体
                finalResult = GetMenuFromJsonResult(jsonResult);
            }
            catch (WeiXinException)
            {
                finalResult = null;
            }
            return(finalResult);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 第三步:刷新access_token(如果需要)
        /// 由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,
        /// refresh_token拥有较长的有效期(7天、30天、60天、90天),当refresh_token失效的后,需要用户重新授权。
        /// </summary>
        /// <param name="appId">公众号的appid</param>
        /// <param name="refreshToken">填写通过第二步获取到的refresh_token参数</param>
        /// <param name="grantType"></param>
        /// <returns></returns>
        public static OAuthAccessTokenResult RefreshToken(string appId, string refreshToken, string grantType = "refresh_token")
        {
            var url =
                string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type={1}&refresh_token={2}",
                              appId, grantType, refreshToken);
            string result = RequestUtility.HttpGet(url, null);

            return(ApiHelper.GetResult <OAuthAccessTokenResult>(result));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 第二步:获取AccessToken
        /// 用户访问第一步网页授权页面后,无论同意或拒绝,都会返回redirectUrl页面。
        /// 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。
        /// 若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
        /// </summary>
        /// <param name="appId">公众号的appid</param>
        /// <param name="secret">公众号的appsecret</param>
        /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,
        /// code只能使用一次,5分钟未被使用自动过期。</param>
        /// <param name="grantType">目前只有authorization_code一种</param>
        /// <returns></returns>
        public static OAuthAccessTokenResult GetAccessToken(string appId, string secret, string code, string grantType = "authorization_code")
        {
            var url =
                string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}",
                              appId, secret, code, grantType);
            string result = RequestUtility.HttpGet(url, null);

            return(ApiHelper.GetResult <OAuthAccessTokenResult>(result));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取凭证接口
        /// </summary>
        /// <param name="grant_type">获取access_token填写client_credential</param>
        /// <param name="appid">第三方用户唯一凭证</param>
        /// <param name="secret">第三方用户唯一凭证密钥,既appsecret</param>
        /// <returns></returns>
        public static AccessTokenResult GetToken(string appid, string secret, string grant_type = "client_credential")
        {
            //微信公众平台获取access_token接口地址
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
                                       grant_type, appid, secret);
            string result = RequestUtility.HttpGet(url, null);

            return(ApiHelper.GetResult <AccessTokenResult>(result));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 获取小程序的授权信息
        /// </summary>
        /// <param name="appId">小程序appid</param>
        /// <param name="secret">小程序秘钥</param>
        /// <param name="jsCode">登录code</param>
        /// <param name="grantType">授权类型</param>
        /// <returns></returns>
        public static OAuthResult GetOAuthResult(string appId, string secret, string jsCode, string grantType = "authorization_code")
        {
            var url =
                string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code",
                              appId, secret, jsCode, grantType);
            string result = RequestUtility.HttpGet(url, null);

            return(ApiHelper.GetResult <OAuthResult>(result));
        }