微信登录获取openid和access_token 报错 unacceptable content-type: text/plain,

错误代码:

 String requestUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + rongWxSet.getWxAppid() + "&secret=" + rongWxSet.getWxSecret() + "&code=" + wx_code + "&grant_type=authorization_code";
            WxBackResult wxBackResult = restTemplate.getForObject(requestUrl,WxBackResult .class);
            

报错:unacceptable content-type: text/plain,  

主要意思就是访问微信接口后返回的结果不能直接映射到自定义类型中。

解决方法:

String requestUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + rongWxSet.getWxAppid() + "&secret=" + rongWxSet.getWxSecret() + "&code=" + wx_code + "&grant_type=authorization_code";
            String re = restTemplate.getForObject(requestUrl,String.class);
            WxBackResult wxBackResult = null;
            try {
                wxBackResult = objectMapper.readValue(re,WxBackResult.class);
            } catch (IOException e) {
                e.printStackTrace();
            }

其实微信接口返回结果就是标准的json格式的字符串,所以我们先用字符串接受,然后转化成我们的自定义对象。

自定义对象:WxBackResult 

根据微信的返回结果自定义的结果对象

@Data
public class WxBackResult  {

    @ApiModelProperty(value = "微信返回的token")
    private String access_token;

    @ApiModelProperty(value = "调用微信接口返回的openId")
    private String openid;

    private Integer errcode;

    private String errmsg;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值