在网页授权第一步打开链接获取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<String, 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自己也不行