m0_67689474 2022-12-28 17:28 采纳率: 66.7%
浏览 24
已结题

使用springboot进行微信小程序网页授权

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

  • 写回答

2条回答 默认 最新

  • 嗷呜大嘴狼 2022-12-28 18:26
    关注

    根据你提供的信息,在调用网页授权时,你的方法中需要接收两个参数: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,表示这个参数是可选的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月6日
  • 已采纳回答 12月29日
  • 创建了问题 12月28日

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败