在网页授权第一步打开链接获取code,重定向到我自己的方法上但是一直接收不到code
package com.gyt.zhiwei.service.gzh; import com.gyt.zhiwei.constant.WeCharConstant; import com.gyt.zhiwei.pojo.gzh.TextMessage; import com.gyt.zhiwei.utils.Student; import com.gyt.zhiwei.utils.gzh.IMessage; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.Map; /** * Created with IntelliJ IDEA. * User: wanghongjie * Date: 2020/10/18 - 19:29 *
* Description: */ @Service @Slf4j public class TextMessageService implements IMessage { @Override public String handler(Map
Object> stringObjectMap) { String content = "嗨咯你好呀!"; if (stringObjectMap.get(WeCharConstant.CONTENT).toString().equals("登录")) { content = WeCharConstant.OAUTH2_AUTHORIZE.replace("APPID", "wxf64c689b56684a75") .replace("REDIRECT_URI", "http://192.168.1.160:8080/gyt/weChar/getCode") .replace("SCOPE", "snsapi_userinfo"); System.out.println(content); content = "您好,请点击登录进行更多的操作。"; } TextMessage textMessage = TextMessage.ofSendMsg(stringObjectMap, content); return textMessage.toXml(); } }
package com.gyt.zhiwei.controller.gzh; import com.alibaba.fastjson.JSON; import com.gyt.zhiwei.constant.WeCharConstant; import com.gyt.zhiwei.utils.Student; import com.gyt.zhiwei.utils.gzh.HttpClientUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; /** * Created with IntelliJ IDEA. * User: wanghongjie * Date: 2020/11/1 - 09:07 *
* Description: */ @Slf4j @RestController @RequestMapping("/gyt/weChar") public class UserInfoController { /** * 回调链接 * 1、获取code -> 获取accessToken ->获取用户基本信息 */ // @RequestMapping("/getCode") // public String getCode(@RequestParam("code") String code,@RequestParam("code") String state) throws IOException { // System.out.println(code); // System.out.println(state); // String userInfo = getAccessToken(code); // log.info("获取用户的基本信息 【{}】", userInfo); // return userInfo; // } @RequestMapping("/getCode") public void getCode(@RequestParam(name = "code", required = false) String code) { System.out.println("code: "+code); } /** * 获取accessToken 和 openID * * @param code * @return * @throws IOException */ public String getAccessToken(String code) throws IOException { Student.get(); String s = HttpClientUtils.get(WeCharConstant.OAUTH_GET_AT.replace("APPID", Student.getAppid()) .replace("SECRET", Student.getSecret()).replace("CODE", code)); System.out.println("获取access token " + s); String access_token = JSON.parseObject(s).getString("access_token"); String openid = JSON.parseObject(s).getString("openid"); return getUserInfo(access_token, openid); } /** * 获取用户基本信息 * * @param accessToken * @param openId * @return * @throws IOException */ public String getUserInfo(String accessToken, String openId) throws IOException { return HttpClientUtils.get(WeCharConstant.OAUTH_USER_INFO.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId)); } }
不获取accesstoken,只打印接收到的参数也打印不出来,接收code和state两个也不行,code自己也不行
根据你提供的信息,在调用网页授权时,你的方法中需要接收两个参数:code和state,但是你的方法中只声明了一个参数code,导致state参数接收不到,导致程序执行出错。
为了解决这个问题,你需要在方法中声明state参数,并且给它赋默认值,这样就可以解决问题了。
修改后的代码如下:
@RequestMapping("/getCode")
public void getCode(@RequestParam(name = "code", required = false) String code,
@RequestParam(name = "state", required = false) String state) throws IOException {
System.out.println(code);
System.out.println(state);
String userInfo = getAccessToken(code);
log.info("获取用户的基本信息 【{}】", userInfo);
}
在这里,我们给code和state两个参数都赋了默认值,这样就不会抛出空指针异常了。
此外,你还需要注意,如果你的方法中使用了@RequestParam注解来接收参数,那么这个参数是必须的,如果没有传入,那么就会抛出"Required String parameter 'code' is not present"的异常。如果你希望这个参数可以不传,那么可以将@RequestParam的required属性设为false,表示这个参数是可选的。