Rails 微信网页授权失败的问题

sforce100 · June 16, 2014 · Last by jeff_duan replied at June 24, 2014 · 8145 hits

使用了一下两个 gem 做微信的网页授权,

gem 'omniauth'
gem 'omniauth-weixin'

奇怪的是当我点击回调的连接以后,微信向我服务器发送了三次回调,前两次连报错都没有,但是第三次是成功的。但最后微信页面上显示找不到页面。

回调地址后面加上#wechat_redirect

@dddd1919 已经加了

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxx&redirect_uri=http://callbak.com&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

或者是你的服务相应太慢了

号认证了吗?

@sforce100 ,话说做开放授权别用那些特定的 GEM,只要一个 Oauth2 稍加修改就 ok 了, 这个是我写的修改后的用法,其实只要改两个属性就好了。

1.安装 oauth2 Gemfile gem 'oauth2', :git => 'https://github.com/oldfritter/oauth2' 2.业务代码范例 Ruby 代码 收藏代码 require 'oauth2'
@client = OAuth2::Client.new my.key, my.sercet, site: 'https://api.weibo.com'
@client.options[:authorize_url] = '/oauth2/authorize'
@client.options[:token_url] = '/oauth2/token'
(注:由于默认的认证是提交到 site/oauth/authorize,而新浪微博是https://api.weibo.com/oauth2/authorize, 所以就有了上面两行) Ruby 代码 收藏代码 获取授权 code
@client.auth_code.authorize_url redirect_uri: call_back_url # call_back_url 是你的回调 url

获取授权的 access_token

@token = @client.auth_code.get_token code, redirect_uri: call_back_url

向 Weibo 推送一条消息

@token.post '/2/statuses/update.json', params: {'access_token' => @token.token, 'status' => 'test message'}

原文:http://oldfritter.iteye.com/blog/2068210

@oldfritter 谢谢你的提示,你这方法也不错。但是我的需求比较简单就是可以直接再微信菜单打开网页(需要验证绑定过,所以需要 openid),所以我通过网页授权那个 openid。不知道为什么拿 access token 和获取用户信息这一步很慢,甚至超过 5s。后来调整一下流程,只用 scope=snsapi_base,还有先验证是否登录过才跳授权。

你说的这个流程就是再正常不过的微信 oauth2 流程. 首先要确保你在微信菜单里的回调 url 带参数都要 url encoding. 其次,token 必须设置正确。

一般情况点击了餐单会立即弹出 oauth 授权页面,这一步应该相当快。点击确认后,如果你从这里开始很慢,说明你的逻辑或者各种 key 有错,微信在尝试 5s 重试。

我做过的速度很快啊,难道你的服务器在国外?

你可以试试我的 www(dot)88jimo(dot)com

You need to Sign in before reply, if you don't have an account, please Sign up first.