收藏
回答

请求openid,始终返回40163

问题模块
API和组件


这个app.js的部分内容

App({
 onLaunch: function () {
   //调用API从本地缓存中获取数据
   var logs = wx.getStorageSync('logs') || []
   logs.unshift(Date.now())
   wx.setStorageSync('logs', logs);
   this.loginFunction();
 },
 loginFunction:function(){
   wx.checkSession({
     // success: function () {
     //   console.log("checksession success")
     //   //session 未过期,并且在本生命周期一直有效
     // },
     success: function () {
       wx.login({
         success: function (res) {
           if (res.code) {
             //发起网络请求
             wx.request({
               url: "https://myyxa17.hk1.mofasuidao.cn/ServletTest/HelloWorldServlet",
               data:{
                 code:res.code
               },
               success: function (res) {
                 console.log(res);
               },
               fail: function (res) {
                 console.log(res);
               }
             })
           } else {
             console.log('获取用户登录态失败!' + res.errMsg)
           }
         }
       })
     }
   })
 }}

这个是servlet内容

@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
     
   /**
    * @see HttpServlet#HttpServlet()
    */
   public HelloWorldServlet() {
       super();
       // TODO Auto-generated constructor stub
   }
   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       String code="";
       code=request.getParameter("code");
       System.out.println(code);
       String url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET"
               + "&grant_type=authorization_code&js_code="+code;
       String wxUserInfoJsonStr = HttpClientUtil.getInstance().sendHttpsGet(url);
       response.getWriter().append("Served at: ").append(wxUserInfoJsonStr);
   }
   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       doGet(request, response);
   }
}

直接在后台打断点,第一次请求就直接返回40163,求解,看别人都是5分钟之内重复使用code才会返回40163,我这个怎么始终返回40163呢,应该怎么修改

最后一次编辑于  2017-08-29  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏投诉关注问题回答

5 个回答

  • 宏哲
    宏哲
    2017-09-13

    大哥你都没说一下怎么解决

    2017-09-13
    赞同
    回复
  • 饭团
    饭团
    2017-09-13

    换了下发送GET请求的方式就行了,

    2017-09-13
    赞同
    回复
  • 月夜枫
    月夜枫
    2017-11-18

    必须使用POST请求才能够成功。


    public static String sendHttpsPost(String url, Map<String, String> params) throws IOException, GeneralSecurityException {

    String responseContent = null;

    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> nvps = new ArrayList<>();

    for (Map.Entry<String, String> entry : params.entrySet()) {

    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

    }

    httpPost.setEntity(new UrlEncodedFormEntity(nvps, Constant.UTF_8));

    CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();

    CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

    HttpEntity httpEntity = httpResponse.getEntity();

    if (httpEntity != null) {

    responseContent = EntityUtils.toString(httpEntity, Constant.UTF_8);

    }

    return responseContent;

    }


    private static SSLConnectionSocketFactory createSSLConnSocketFactory() throws GeneralSecurityException {

    SSLConnectionSocketFactory sslsf = null;

    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {


    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {

    return true;

    }

    }).build();

    sslsf = new SSLConnectionSocketFactory(sslContext);


    return sslsf;

    }


    2017-11-18
    赞同
    回复
  • 月夜枫
    月夜枫
    2017-11-18

    String urlPath = new String("https://api.weixin.qq.com/sns/jscode2session"); // 微信提供的API,这里最好也放在配置文件

    Map<String, String> params = new HashMap<>();

    params.put("appid", appId);

    params.put("secret", secret);

    params.put("js_code", code);

    params.put("grant_type", "authorization_code"); // 固定值

    params.put("r", ""); // 随机值

    String data = HttpClientTest.sendHttpsPost(urlPath, params); // java的网络请求,这里是我自己封装的一个工具包,返回字符串


    2017-11-18
    赞同
    回复
  • Mr. Chen
    Mr. Chen
    2018-08-06

    如何解决的

    2018-08-06
    赞同
    回复