wx.login获取openId一定要把appid那些参数写在url里面?
发布于 6 年前 作者 nlong 3639 次浏览 来自 问答

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

谢邀,直接拼,就OK。APPID和秘钥我都弄的假的。

楼主我想资讯下,我在本地通过后台方法拿到appid了。提交体验版时就获取不到,这个是什么原因。是因为我们的服务器访问不了外网么

回到顶部