Java 微信公众号网页授权
来源:互联网 发布:ubuntu编写shell脚本 编辑:程序博客网 时间:2024/06/07 06:02
写这篇文章之前简单说下,本人之前也没做过微信这方面的开发,也是属于第一次接触微信公众号开发,写下这篇博客也是为了记录自己的开发内容,也算是给微信公共号开发小白一些借鉴,不喜勿喷。好了 废话不多说
首先登录微信公众号----点击左侧列表接口权限----网页服务----网页授权(网页授权获取用户基本信息)-----网页授权域名(这个域名一定是备案过的)
上面的操作完成之后
看一下微信网页授权的API 地址为https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&lang=zh_CN
仔细看下上面图片中的参数说明
https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=回调地址&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
这个回调的地址就是你的域名/项目名称/你的服务/方法
例如楼主的redirect_uri为http://域名/MountainTourism/wechat/getOpenid (切记这个上面一定要加http:// 不能单独写域名 楼主在这吃亏了)
好了以上就是需要设置的地方,因为楼主的业务是需要网页授权之后返回openid,接下来直接上代码
WeChatController.java
/** * */package com.htht.tourism.action;import java.io.IOException;import javax.annotation.Resource;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;import me.chanjar.weixin.mp.bean.result.WxMpUser;import org.springframework.stereotype.Controller;import org.springframework.validation.BindingResult;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.htht.tourism.domain.Response;import com.htht.tourism.domain.User;import com.htht.tourism.domain.link.UserParam;import com.htht.tourism.intf.TUserService;import com.htht.tourism.utils.ValidatorResultHandler;import com.htht.tourism.utils.WeixinHelper;/** * @author wuqiwei * */@Controller@ResponseBody@RequestMapping("/wechat")public class WeChatController {@Resourceprivate TUserService tUserService;/** * 获取openid * @author:wuqiwei * @param @param param * @param @param result * @param @return * @throws IOException * @throws ServletException * @date:2017年3月27日下午2:35:13 */@RequestMapping(value = "/getOpenid", method = RequestMethod.GET)public ModelAndView add(ModelAndView model,final HttpServletRequest request,final HttpServletResponse response) throws ServletException, IOException {WxMpUser wxMpUser = null;try {System.out.println(request.getParameter("code"));WxMpOAuth2AccessToken wxMpOAuth2AccessToken = WeixinHelper.getWxMpService().oauth2getAccessToken(request.getParameter("code"));wxMpUser = WeixinHelper.getWxMpService().oauth2getUserInfo(wxMpOAuth2AccessToken, null);} catch (Exception e) {e.printStackTrace();}//先判断wxMpUser是否为null//if (wxMpUser != null) {////request.getRequestDispatcher("index.jsp").forward(request,response); //请求转发////return Response.getInstance().success("1");//}ModelAndView mav = new ModelAndView("index");mav.addObject("openid", wxMpUser.getOpenId());return mav;}}WeixinHelper.java
package com.htht.tourism.utils;import java.util.HashMap;import java.util.Map;import me.chanjar.weixin.common.bean.WxJsapiSignature;import me.chanjar.weixin.common.exception.WxErrorException;import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;import me.chanjar.weixin.mp.api.WxMpService;import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;/** * WeixinHelper */public final class WeixinHelper { private WeixinHelper() { throw new Error("工具类不能实例化!"); }// private static final String CONFIG_URL = "application.properties"; public static final String APPID = ConfigUtil.getProperty("WeChat","appid"); // appid public static final String APPSECRET = ConfigUtil.getProperty("WeChat","appsecret"); // appsecret public static final String PATERNER_KEY =ConfigUtil.getProperty("WeChat","paternerKey"); // 商户支付密钥 public static final String MCHI_ID = ConfigUtil.getProperty("WeChat","mch_id"); // 商户号 public static final String NOTIFY_URL = ConfigUtil.getProperty("WeChat","notify_url");; // 回调地址 public static final String TRADE_TYPE_JS = ConfigUtil.getProperty("WeChat","trade_type_js"); // 交易类型 public static final String WEIXIN_OAUTH2 = ConfigUtil.getProperty("WeChat","WEIXIN_OAUTH2"); // 回调地址 private static WxMpService wxMpService = null; /** * 获取WxMpService */ public static WxMpService getWxMpService() { if (wxMpService == null) { WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage(); config.setAppId(APPID); // 设置微信公众号的appid config.setSecret(APPSECRET); // 设置微信公众号的app corpSecret // config.setToken("..."); // 设置微信公众号的token // config.setAesKey("..."); // 设置微信公众号的EncodingAESKey config.setOauth2redirectUri(WEIXIN_OAUTH2); wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(config); } return wxMpService; } /** * getShortUrl */ public static String getShortUrl(final String url) { String shortUrl = url; try { shortUrl = getWxMpService().shortUrl(url); } catch (WxErrorException e) { e.printStackTrace(); } return shortUrl; } /** * getJsapiTicket */ public static String getJsapiTicket() throws WxErrorException { return getWxMpService().getJsapiTicket(); } /** * createJsapiSignature */ public static WxJsapiSignature createJsapiSignature(final String url) throws WxErrorException { return getWxMpService().createJsapiSignature(url); } /** * getJSSDKPayInfo */// public Map<String, String> getJSSDKPayInfo(final String body, final String totalFee) throws Exception {// String outTradeNum = System.currentTimeMillis() + ""; // 商户订单号// Map<String, String> paramMap = new HashMap<String, String>();// paramMap.put("paternerKey", PATERNER_KEY); //// paramMap.put("mch_id", MCHI_ID); //// paramMap.put("body", body); // 商品描述// paramMap.put("out_trade_no", outTradeNum); // 商户端对应订单号// paramMap.put("total_fee", totalFee + "");// paramMap.put("spbill_create_ip", SystemHelper.getCurrentUserIp());// paramMap.put("notify_url", NOTIFY_URL);// paramMap.put("trade_type", TRADE_TYPE_JS);// Map<String, String> payInfo = WeixinHelper.getWxMpService().getJSSDKPayInfo(paramMap);// return payInfo;// }}
这样就可以获取到openid了 返回到前台
以上就是网页授权获取openid的方法 由于是第一次写这种博客 写的不是很好 请见谅
2 0
- Java 微信公众号网页授权
- 微信公众号 网页授权开发
- 微信公众号开发12公众号网页授权
- 公众号网页授权接口开发,实现微信公众号网页授权登录
- java二次开发微信公众号授权
- 微信公众号 授权登录 JAVA
- java微信公众号支付授权
- 微信公众号 授权登录 JAVA
- 微信公众号网页授权获取用户基本信息
- 微信公众平台获取网页授权(测试号)
- 微信公众号——OAuth2.0网页授权
- 微信公众号-获取用户信息(网页授权获取)
- flask 微信公众号 网页授权获取用户基本信息
- 微信公众平台获取网页授权(测试号)
- 微信公众号网页授权登录wordpress
- 微信公众号获取用户信息-网页授权
- 微信公众号开发之OAuth2.0网页授权
- 微信公众号网页授权code been use问题
- 基于MVC的JavaScript_Web富应用开发
- 软件架构设计
- GIT 命令学习:基础快照-add,status,commit,reset,rm,mv,diff
- 未雨绸缪:理解软件配置管理(第2版)
- 验证手机号和设置60秒的倒计时
- Java 微信公众号网页授权
- bbed 找回被删除数据
- Xshell设置grep关键字高亮
- 人人都是产品经理
- Linux 命令 练习 —01
- jvm学习
- Spring(四):Scope作用域
- 神一样的产品经理
- Android中Dialog的用法