me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken Java Examples

The following examples show how to use me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseWxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) throws WxErrorException {
  if (lang == null) {
    lang = "zh_CN";
  }

  String url = String.format(WxMpService.OAUTH2_USERINFO_URL, token.getAccessToken(), token.getOpenId(), lang);

  try {
    RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
    String responseText = executor.execute(url, null);
    return WxMpUser.fromJson(responseText);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #2
Source File: WxMpOAuth2AccessTokenAdapter.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
public WxMpOAuth2AccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws
    JsonParseException {
  WxMpOAuth2AccessToken accessToken = new WxMpOAuth2AccessToken();
  JsonObject accessTokenJsonObject = json.getAsJsonObject();

  if (accessTokenJsonObject.get("access_token") != null && !accessTokenJsonObject.get("access_token").isJsonNull()) {
    accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
  }
  if (accessTokenJsonObject.get("expires_in") != null && !accessTokenJsonObject.get("expires_in").isJsonNull()) {
    accessToken.setExpiresIn(GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expires_in")));
  }
  if (accessTokenJsonObject.get("refresh_token") != null && !accessTokenJsonObject.get("refresh_token").isJsonNull()) {
    accessToken.setRefreshToken(GsonHelper.getAsString(accessTokenJsonObject.get("refresh_token")));
  }
  if (accessTokenJsonObject.get("openid") != null && !accessTokenJsonObject.get("openid").isJsonNull()) {
    accessToken.setOpenId(GsonHelper.getAsString(accessTokenJsonObject.get("openid")));
  }
  if (accessTokenJsonObject.get("scope") != null && !accessTokenJsonObject.get("scope").isJsonNull()) {
    accessToken.setScope(GsonHelper.getAsString(accessTokenJsonObject.get("scope")));
  }
  if (accessTokenJsonObject.get("unionid") != null && !accessTokenJsonObject.get("unionid").isJsonNull()) {
    accessToken.setUnionId(GsonHelper.getAsString(accessTokenJsonObject.get("unionid")));
  }
  return accessToken;
}
 
Example #3
Source File: WxMpOAuth2AccessTokenAdapter.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpOAuth2AccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws
  JsonParseException {
  WxMpOAuth2AccessToken accessToken = new WxMpOAuth2AccessToken();
  JsonObject accessTokenJsonObject = json.getAsJsonObject();

  if (accessTokenJsonObject.get("access_token") != null && !accessTokenJsonObject.get("access_token").isJsonNull()) {
    accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
  }
  if (accessTokenJsonObject.get("expires_in") != null && !accessTokenJsonObject.get("expires_in").isJsonNull()) {
    accessToken.setExpiresIn(GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expires_in")));
  }
  if (accessTokenJsonObject.get("refresh_token") != null && !accessTokenJsonObject.get("refresh_token").isJsonNull()) {
    accessToken.setRefreshToken(GsonHelper.getAsString(accessTokenJsonObject.get("refresh_token")));
  }
  if (accessTokenJsonObject.get("openid") != null && !accessTokenJsonObject.get("openid").isJsonNull()) {
    accessToken.setOpenId(GsonHelper.getAsString(accessTokenJsonObject.get("openid")));
  }
  if (accessTokenJsonObject.get("scope") != null && !accessTokenJsonObject.get("scope").isJsonNull()) {
    accessToken.setScope(GsonHelper.getAsString(accessTokenJsonObject.get("scope")));
  }
  if (accessTokenJsonObject.get("unionid") != null && !accessTokenJsonObject.get("unionid").isJsonNull()) {
    accessToken.setUnionId(GsonHelper.getAsString(accessTokenJsonObject.get("unionid")));
  }
  return accessToken;
}
 
Example #4
Source File: BaseWxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException {
  try {
    RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
    String responseText = executor.execute(url, null);
    return WxMpOAuth2AccessToken.fromJson(responseText);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #5
Source File: WxMpOAuth2Servlet.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  response.setStatus(HttpServletResponse.SC_OK);

  String code = request.getParameter("code");
  try {
    response.getWriter().println("<h1>code</h1>");
    response.getWriter().println(code);

    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
    response.getWriter().println("<h1>access token</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

    WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
    response.getWriter().println("<h1>user info</h1>");
    response.getWriter().println(wxMpUser.toString());

    wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
    response.getWriter().println("<h1>after refresh</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

  } catch (WxErrorException e) {
    e.printStackTrace();
  }

  response.getWriter().flush();
  response.getWriter().close();

}
 
Example #6
Source File: WxRedirectController.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/greet")
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {

	WxMpService mpService = WxMpConfiguration.getMpServices().get(appid);

	try {
		WxMpOAuth2AccessToken accessToken = mpService.oauth2getAccessToken(code);
		WxMpUser user = mpService.oauth2getUserInfo(accessToken, null);
		map.put("user", user);
	} catch (WxErrorException e) {
		e.printStackTrace();
	}

	return "greet_user";
}
 
Example #7
Source File: WxMpOAuth2Servlet.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
  throws IOException {

  response.setContentType("text/html;charset=utf-8");
  response.setStatus(HttpServletResponse.SC_OK);

  String code = request.getParameter("code");
  try {
    response.getWriter().println("<h1>code</h1>");
    response.getWriter().println(code);

    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = this.wxMpService.oauth2getAccessToken(code);
    response.getWriter().println("<h1>access token</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

    WxMpUser wxMpUser = this.wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
    response.getWriter().println("<h1>user info</h1>");
    response.getWriter().println(wxMpUser.toString());

    wxMpOAuth2AccessToken = this.wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
    response.getWriter().println("<h1>after refresh</h1>");
    response.getWriter().println(wxMpOAuth2AccessToken.toString());

  } catch (WxErrorException e) {
    e.printStackTrace();
  }

  response.getWriter().flush();
  response.getWriter().close();

}
 
Example #8
Source File: BaseWxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
  String url = String.format(WxMpService.OAUTH2_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret(), code);
  return this.getOAuth2AccessToken(url);
}
 
Example #9
Source File: WechatRabbitListener.java    From fw-cloud-framework with MIT License 4 votes vote down vote up
@RabbitHandler
public void receive(WechatNotifyBean wechatNotifyBean) {
	long start = System.currentTimeMillis();
	log.info("【wxauth.wechatRabbit】:exec receive start|" + start);
	WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wechatNotifyBean.getWxMpOAuth2AccessToken();
	if (null == wxMpOAuth2AccessToken || StringHelper.isBlank(wechatNotifyBean.getWechatId())
			|| StringHelper.isBlank(wxMpOAuth2AccessToken.getOpenId())) return;
	log.info("【wxauth.wechatRabbit】:exec start");
	try {
		WxMpUser wxMpUser = null;
		String openId = wxMpOAuth2AccessToken.getOpenId();
		AuthInfo authInfo = authInfoService.findByOpenIdAndWechatId(openId, wechatNotifyBean
				.getWechatId());
		if (null == authInfo // 大于2小时
				|| System.currentTimeMillis() - authInfo.getUpdateTime().getTime() > 7200000) {
			boolean isSopeBase = wechatNotifyBean.isSopeBase();
			if (isSopeBase) {
				log.info("【wxauth.openId】静默登录");
				wxMpUser = wxService.getUserService().userInfo(openId);
			} else {
				// refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
				String refreshToken = wxMpOAuth2AccessToken.getRefreshToken();
				wxMpOAuth2AccessToken = wxService.oauth2refreshAccessToken(refreshToken);
				log.info("【wxauth.openId】主动登录");
				// 拉取用户信息(需scope为 snsapi_userinfo)
				wxMpUser = wxService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
			}

			if (null != wxMpUser) {
				if (null == authInfo) authInfo = new AuthInfo();
				authInfo.setOpenId(openId);
				authInfo.setWechatId(wechatNotifyBean.getWechatId());
				authInfo.setNickName(WebUtils.buildURLEncoder(EmojiUtils.toHtml(wxMpUser
						.getNickname())));
				authInfo.setHeadImgUrl(wxMpUser.getHeadImgUrl());
				authInfo.setCity(wxMpUser.getCity());
				authInfo.setProvince(wxMpUser.getProvince());
				authInfo.setLanguage(wxMpUser.getLanguage());
				authInfo.setRemark(wxMpUser.getRemark());
				authInfo.setSexDesc(wxMpUser.getSexDesc());
				authInfo.setSex(wxMpUser.getSex());
				authInfo.setCountry(wxMpUser.getCountry());
				authInfo.setRefreshToken(wxMpOAuth2AccessToken.getRefreshToken());
				if (null == authInfo.getCreateTime()) authInfo.setCreateTime(new Date());
				authInfo.setUpdateTime(new Date());
				authInfoService.saveOrUpdate(authInfo);
			}
		}
		log.info("【wxauth.wechatRabbit】:openId|" + openId);
	} catch (WxErrorException ex) {
		ex.printStackTrace();
		log.info("【wxauth.wechatRabbit】exception:" + ex.getError().getErrorMsg());
	}
	log.info("【wxauth.wechatRabbit】:exec finished 耗时:" + (System.currentTimeMillis() - start));
}
 
Example #10
Source File: WxOpenMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
  return wxOpenComponentService.oauth2refreshAccessToken(appId, refreshToken);
}
 
Example #11
Source File: WxOpenMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
  return wxOpenComponentService.oauth2getAccessToken(appId, code);
}
 
Example #12
Source File: WxOpenComponentServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String appId, String refreshToken) throws WxErrorException {
  String url = String.format(OAUTH2_REFRESH_TOKEN_URL, appId, refreshToken, getWxOpenConfigStorage().getComponentAppId());
  String responseContent = get(url);
  return WxMpOAuth2AccessToken.fromJson(responseContent);
}
 
Example #13
Source File: WxOpenComponentServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpOAuth2AccessToken oauth2getAccessToken(String appId, String code) throws WxErrorException {
  String url = String.format(OAUTH2_ACCESS_TOKEN_URL, appId, code, getWxOpenConfigStorage().getComponentAppId());
  String responseContent = get(url);
  return WxMpOAuth2AccessToken.fromJson(responseContent);
}
 
Example #14
Source File: BaseWxMpServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
  String url = String.format(WxMpService.OAUTH2_REFRESH_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), refreshToken);
  return this.getOAuth2AccessToken(url);
}
 
Example #15
Source File: WxMpService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 验证oauth2的access token是否有效
 * </pre>
 */
boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);
 
Example #16
Source File: WxMpService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以
 * </pre>
 *
 * @param lang zh_CN, zh_TW, en
 */
WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;
 
Example #17
Source File: WxMpService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 刷新oauth2的access token
 * </pre>
 */
WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException;
 
Example #18
Source File: WxMpService.java    From weixin-java-tools with Apache License 2.0 2 votes vote down vote up
/**
 * <pre>
 * 用code换取oauth2的access token
 * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
 * </pre>
 */
WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException;
 
Example #19
Source File: WxOpenComponentService.java    From weixin-java-tools with Apache License 2.0 votes vote down vote up
WxMpOAuth2AccessToken oauth2refreshAccessToken(String appid, String refreshToken) throws WxErrorException; 
Example #20
Source File: WxOpenComponentService.java    From weixin-java-tools with Apache License 2.0 votes vote down vote up
WxMpOAuth2AccessToken oauth2getAccessToken(String appid, String code) throws WxErrorException;