/// <summary> /// 获取 openid /// </summary> /// <param name="appId">微信公众号应用唯一标识</param> /// <param name="appSecret">微信公众号应用密钥(注意保密)</param> /// <param name="code">获取到的 code</param> /// <returns>openid</returns> public static String getOpenId(String appId, String appSecret, String code) { String url = WxPubUtils.createOauthUrlForOpenid(appId, appSecret, code); String ret = GetRequest(url); OAuthResult oAuthResult = Mapper <OAuthResult> .MapFromJson(ret); return(oAuthResult.Openid.ToString()); }
/// <summary> /// 生成获取 openid 的 URL 地址 /// </summary> /// <param name="appId">微信公众号应用唯一标识</param> /// <param name="appSecret">微信公众号应用密钥(注意保密)</param> /// <param name="code">获取到的 code</param> /// <returns>获取 openid 的 URL 地址</returns> private static String createOauthUrlForOpenid(String appId, String appSecret, String code) { Dictionary <String, String> data = new Dictionary <String, String>(); data.Add("appid", appId); data.Add("secret", appSecret); data.Add("code", code); data.Add("grant_type", "authorization_code"); String queryString = WxPubUtils.httpBuildQuery(data); return(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?{0}", queryString)); }
/// <summary> /// 用于生成获取授权 code 的 URL 地址,此地址用于用户身份鉴权,获取用户身份信息,同时重定向到 redirect_url /// </summary> /// <param name="appId">微信公众号应用唯一标识</param> /// <param name="redirectUrl"> /// 授权后重定向的回调链接地址,重定向后此地址将带有授权code参数,该地址的域名需在微信公众号平台上进行设置, /// 步骤为:登陆微信公众号平台 开发者中心 网页授权获取用户基本信息 修改 /// </param> /// <param name="moreInfo"> /// FALSE 不弹出授权页面,直接跳转,这个只能拿到用户openid /// TRUE 弹出授权页面,这个可以通过 openid 拿到昵称、性别、所在地 /// </param> /// <returns>用于获取授权 code 的 URL 地址</returns> public static String createOauthUrlForCode(String appId, String redirectUrl, Boolean moreInfo) { Dictionary <String, String> data = new Dictionary <String, String>(); data.Add("appid", appId); data.Add("redirect_uri", redirectUrl); data.Add("response_type", "code"); data.Add("scope", moreInfo ? "snsapi_userinfo" : "snsapi_base"); data.Add("state", "STATE#wechat_redirect"); String queryString = WxPubUtils.httpBuildQuery(data); return("https://open.weixin.qq.com/connect/oauth2/authorize?" + queryString); }
/// <summary> /// 生成微信公众号 jsapi_ticket /// </summary> /// <param name="appId">微;\信公众号应用唯一标识</param> /// <param name="appSecret">微信公众号应用密钥(注意保密)</param> /// <returns>Ticket</returns> public static String getJsapiTicket(String appId, String appSecret) { Dictionary <String, String> data = new Dictionary <String, String>(); data.Add("appid", appId); data.Add("secret", appSecret); data.Add("grant_type", "client_credential"); String queryString = WxPubUtils.httpBuildQuery(data); String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?" + queryString; String resp = GetRequest(accessTokenUrl); var jObject = JObject.Parse(resp); data.Clear(); data.Add("access_token", jObject.GetValue("access_token").ToString()); data.Add("type", "jsapi"); queryString = WxPubUtils.httpBuildQuery(data); String jsapiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?" + queryString; resp = GetRequest(jsapiTicketUrl); var ticket = JObject.Parse(resp); return(ticket.GetValue("ticket").ToString()); }