Java代公众号发起网页授权

编程入门 行业动态 更新时间:2024-10-10 02:22:49

Java代<a href=https://www.elefans.com/category/jswz/34/1769853.html style=公众号发起网页授权"/>

Java代公众号发起网页授权

写这篇文章之前简单说下,本人之前也没做过微信这方面的开发,也是属于第一次接触微信公众号开发,写下这篇博客也是为了记录自己的开发内容,也算是给微信公共号开发小白一些借鉴,不喜勿喷。好了 废话不多说

首先登录微信公众号----点击左侧列表接口权限----网页服务----网页授权(网页授权获取用户基本信息)-----网页授权域名(这个域名一定是备案过的)

上面的操作完成之后

看一下微信网页授权的API 地址为=resource/res_main&id=mp1421140842&token=&lang=zh_CN

仔细看下上面图片中的参数说明

=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

*

*/

@ResponseBody

@RequestMapping("/wechat")

public class WeChatController {

private 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.weixinmon.bean.WxJsapiSignature;

import me.chanjar.weixinmon.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 getJSSDKPayInfo(final String body, final String totalFee) throws Exception {

//        String outTradeNum = System.currentTimeMillis() + ""; // 商户订单号

//        Map paramMap = new HashMap();

//        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 payInfo = WeixinHelper.getWxMpService().getJSSDKPayInfo(paramMap);

//        return payInfo;

//    }

}

这样就可以获取到openid了 返回到前台

以上就是网页授权获取openid的方法 由于是第一次写这种博客 写的不是很好 请见谅

更多推荐

Java代公众号发起网页授权

本文发布于:2024-03-23 20:42:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1742599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:公众   网页   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!