Rails 微信 API 超时

hxh1246996371 · 2015年07月08日 · 最后由 kevin_isky 回复于 2017年08月04日 · 3540 次阅读

我在网页授权获取 access_token 这一步发了一个 post 请求。但是微信老是返回超时。服务器是部署在阿里云上的,做了负载均衡。

_uri = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{Rails.application.config.wechart_renren_appid}&secret=#{Rails.application.config.wechart_renren_appsecret}&code=#{params[:code]}&grant_type=authorization_code"
_url = URI(_uri)
_http = Net::HTTP.new(_url.host, _url.port) 
_http.use_ssl = true
 _req = _http.post(_url.request_uri,'');

我在服务器终端去 curl 这个地址,都能得到返回的。找了很久都没找到原因,各位有谁遇到过类似问题吗?

这个接口要用 GET 请求,curl 如果没有指定参数的话也是默认的 GET 请求吧。

#2 楼 @hxh1246996371 试了一下,果然都可以,但是两种方法都没有超时,用的 RestClient 发送的请求。

#3 楼 @w7938940 RestClient 应该也是调用的 Net::HTTP 吧 能麻烦你把代码贴出来看看吗

#4 楼 @hxh1246996371

response = RestClient::Request.execute({
    :method => :get,
    :url => "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{appid}&secret=#{secret}&code=#{code}&grant_type=authorization_code"
      })
response = ActiveSupport::JSON.decode(response)

#5 楼 @w7938940 好的,谢谢 我试试

@hxh1246996371 请问这个问题最后解决了么?是什么原因引起的呢?

#7 楼 @dabing1022 实在不好意思,忘了当初是什么原因造成的

也遇到了后端请求微信 API 超时的问题,一般会先做下链路问题的排查,有时候会是因为线路的节点问题造成的,链路问题具体的排查方法和后续处理我整理到这里了https://github.com/kevin-isky/blog/issues/2

需要 登录 后方可回复, 如果你还没有账号请 注册新账号