微信公共號 40029異常個人解決方案
阿新 • • 發佈:2019-01-26
在使用測試號開發時,獲取使用者基本資訊可以正常執行,當使用服務號時,出現 異常:{"errcode":40029,"errmsg":"invalid code"},請求路徑是:
然後再次對照幫助文件,請求路徑以及引數都正確,在百度之後發現,看到比較多的是這樣說明的:
問題出現在redirect_uri=REDIRECT_URI引導使用者點選連結該地址的時候,微信會發出兩次轉向至redirect_uri的相同請求,前面一次被執行緒強制終止,生效的為第二次, 而第一次已經發出與微信獲取code的請求導致微信發出的第二次請求code過期。 在測試中,個人也驗證了這種說法,確實是請求了兩次,解決方法就是,在進行OAuth驗證時候,加上try,catch就Ok了,如下:
@RequestMapping(method = RequestMethod.GET) public void OAuth(HttpServletRequest request,HttpServletResponse response) throws Exception{ try { PrintWriter out = response.getWriter(); response.setContentType("text/html; charset=utf-8"); String code = request.getParameter("code"); String type = request.getParameter("type"); HttpSession session = request.getSession(); StringBuffer sb = null; if (!"authdeny".equals(code)) { } out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } }