/// <summary> /// 通过appID和appsecret获取Access_token /// </summary> /// <returns></returns> private static MAccessToken GetAccesstokenFromWX() { try { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); //// 微信平台的APPID string appid = WebConfigeOpert.GetWXappid(); //// 微信平台的密码 string secret = WebConfigeOpert.GetWXAppSecret(); string strUrl = wxAPIURL + "cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; MAccessToken mode = PublicTools.HttpGetRequest <MAccessToken>(strUrl); LogOpert.AddWeiXinMessage("获取wxAccesstoken结果:" + mode.access_token); return(mode); } catch (Exception ex) { LogOpert.AddWeiXinMessage("获取wxAccesstoken异常:" + ex); } return(null); }
/// <summary> /// 根据code获取openid(一个code只能查询一次) /// </summary> /// <param name="code"></param> /// <returns></returns> public String GetOpenIdByCode(String code) { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); //// 微信平台的APPID string appid = WebConfigeOpert.GetWXappid(); //// 微信平台的密码 string secret = WebConfigeOpert.GetWXAppSecret(); secret = string.IsNullOrEmpty(secret) ? "7233eefb8657eb0f6a1ed7224fdc0e7f" : secret; // 换取access_token 其中包含了openid // 这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。 String strUrl = string.Format("{0}sns/oauth2/access_token?appid={1}&secret={2}&code={3}&grant_type=authorization_code", wxAPIURL, appid, secret, code); MWXUserSampleInfo model = null; model = PublicTools.HttpGetRequest <MWXUserSampleInfo>(strUrl); if (model != null) { return(model.openid); } return(string.Empty); }