/// <summary> /// 订阅号 通过code去腾讯的微信后台换取access_token和openid /// </summary> /// <param name="code"></param> /// <returns></returns> public static string GetOpenId(string code) { string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + MiscConstants.APP_ID + "&secret=" + MiscConstants.APP_SECRET + "&code=" + code + "&grant_type=authorization_code"; HttpUtil httpUtil = new HttpUtil(); string jsonString = httpUtil.ExecuteGet(url); SerializeUtil serializeUtil = new SerializeUtil(); IDictionary<string, string> dict = serializeUtil.ToObject<IDictionary<string, string>>(jsonString); //string accessToken = dict["access_token"]; string openId = dict["openid"]; return openId; }
/// <summary> /// 订阅号/服务号的GetAccessToken /// </summary> /// <returns></returns> private static string GetAccessTokenSubscribe() { string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + MenuUtil.APP_ID + "&secret=" + MenuUtil.APP_SECRET; HttpUtil httpUtil = new HttpUtil(); string result = httpUtil.ExecuteGet(url); SerializeUtil serializeUtil = new SerializeUtil(); IDictionary<string, object> dict = serializeUtil.ToObject<IDictionary<string, object>>(result); string accessToken = (dict["access_token"]).ToString(); //int expiresIn = Convert.ToInt32((dict["expires_in"])); Console.WriteLine("accessToken: {0}", accessToken); return accessToken; }