wx.login获取openId一定要把appid那些参数写在url里面?
wx.login获取openId一定要把appid那些参数写在https://api.weixin.qq.com/sns/jscode2session里面?
4 回复
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ystlfj.oa.config.weixin.WeiXinConfig;
import com.ystlfj.oa.pojo.WeiXinLogin;
import com.ystlfj.oa.service.WeiXinLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
[@Service](/user/Service)("weiXinLoginService")
public class WeiXinLoginServiceImpl implements WeiXinLoginService {
@Autowired
private WeiXinConfig weiXinConfig;
private static Logger logger = LoggerFactory.getLogger(WeiXinLoginServiceImpl.class);
@Override
public WeiXinLogin getOpenId(String code) {
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + weiXinConfig.getAppid() + "&secret=" + weiXinConfig.getSecret() +
"&js_code=" + code + "&grant_type=authorization_code";
RestTemplate template = new RestTemplate();
String result = template.getForObject(url,String.class);
ObjectMapper mapper = new ObjectMapper();
WeiXinLogin weiXinLogin =new WeiXinLogin();
try {
// 将相关key对应的value里的的string转换成对象的实体类集合
weiXinLogin = mapper.readValue(result, WeiXinLogin.class);
} catch (JsonParseException e) {
e.printStackTrace();
logger.error(e.getMessage());
} catch (JsonMappingException e) {
e.printStackTrace();
logger.error(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
// System.out.println(url);
// System.out.println(result);
// System.out.println(weiXinLogin.toString());
return weiXinLogin;
}
}
springboot2.1.6 用的 RestTemplate 发起http连接,weixinconfig 是 配置类获取 appid 和 secret, WeiXinLogin 封装 获取到 openid 和session_key