我自己的微信公众号,通过code获取access_token失败

来源:7-4 换取access_token

qq_夏佐_0

2018-11-18

描述:

我是使用自己的微信公众号,按照课程步骤通过手机微信端访问连接:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5891af4eefa20f6b&redirect_uri=http://buy.natappvip.cc/sell/wx/auth&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
能正常获取code,但是按照课程的方式步骤,将code 传入连接:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx5891af4eefa20f6b&secret=d3102cc3e29750e9ad07bd123926096f&code=CODE&grant_type=authorization_code
获取access-token时,code能正常获取,但是access_token无法获取到,手机端访问时,本地的控制台打印的日志获取code后,后面也没有打印出其他内容,过来许久出现超时:Connection timed out, 具体信息见下面的图片

首先代码如下:

@RestController
@RequestMapping("/wx")
public class WxController {
    private final Logger logger = LoggerFactory.getLogger(WxController.class);

    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code) {
        logger.info("INFO. go to auth function ...");
        logger.info("INFO. code={}",code);
        
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx5891af4eefa20f6b&secret=d3102cc3e29750e9ad07bd123926096f&code=" + code + "&grant_type=authorization_code";
        String url2 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx5891af4eefa20f6b&secret=d3102cc3e29750e9ad07bd123926096f&code=" + code + "&grant_type=authorization_code";

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url,String.class);
        logger.info("INFO. response={}",response);

        String response2 = HttpUtil.get(url2);
        logger.info("INFO. response2={}",response2);
    }
}

通过手机端访问的连接地址也没有错:
因为通过RestTemplate 没有结果,因此我还使用了HttpUtil.get 的方式,结果都是一样

以下是访问截图:

图片描述

本地控制台输出的日志如下:
此时打印了code,说明appid访问的内容是正确的,个人公众号是OK的

图片描述

许久后控制台返回超时的情况:

图片描述

按照公众号说的,获取access_token 需要开放白名单,我也按照文档的做了,还是上面控制的返回结果,下面是我的appid

图片描述

ip白名单设置:一开始我是设置了我自己的公网ip ,以及 buy.natappvip.cc域名放回的公网ip (118.31.62.7) ,但是还是没有效果,于是我设置了内网地址:10.xx ,192.xx (都是虚拟机的地址)
,内网ip肯定不行的,因为微信没法访问这个地址

图片描述

buy.natappvip.cc 的公网地址:

图片描述

buy.natappvip.cc 解析解析虚拟机地址

图片描述

问题:

1、请问师兄,你当时调试是如何调试的?我按照课程调试不通过。。
2、自己的微信公众号,我设置了ip 白名单,同时确认appid 和 secret 100% 正确,那会是什么原因导致的呢?
3、白名单设置 是设置buy.natappvip.cc 解析的ip 118.31.62.7 ? 还是设置有问题?

通过微信公众号调试工具,确认appid 和 secret 是正确的,截图如下:
连接地址:https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=基础支持&form=获取access_token接口 /token&token=&lang=zh_CN

图片描述

写回答

2回答

廖师兄

2018-11-19

同学你好,报出来的错是你的java程序访问

String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx5891af4eefa20f6b&secret=d3102cc3e29750e9ad07bd123926096f&code=" + code + "&grant_type=authorization_code";

超时了,超时的原因是不是电脑网络呢?

你可以打断点,把链接拿出来,用postman请求试试。

0
0

qq_夏佐_0

提问者

2018-11-18

具体报错如下:


2018-11-18 13:41:50,560 - FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms

2018-11-18 13:41:50,657 - INFO. go to auth function ...

2018-11-18 13:41:50,658 - INFO. code=021hYkA8014l1H1fwuy80wO6A80hYkAq

2018-11-18 13:43:06,154 - Servlet.service() for servlet [dispatcherServlet] in context with path [/sell] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://api.weixin.qq.com/sns/oauth2/access_token": Operation timed out (Connection timed out); nested exception is java.net.ConnectException: Operation timed out (Connection timed out)] with root cause

java.net.ConnectException: Operation timed out (Connection timed out)

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

at java.net.Socket.connect(Socket.java:589)

at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)

at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)

at sun.net.NetworkClient.doConnect(NetworkClient.java:180)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)

at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)

at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)

at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)

at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)

at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)

at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)

at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)

at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)

at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)

at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)

at com.itaofly.controller.WxController.auth(WxController.java:34)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)


0
0

Spring Boot双版本(1.5/2.1) 打造企业级微信点餐系统

从0到1开发中小型企业级Java应用,并学会迭代重构技巧

6410 学习 · 5247 问题

查看课程