本文介绍通用的使用第三方接入的方法,可以类似做出接入其它网站(如QQ等,注:目前QQ自己做的比较乱,观望之)
应用场景例子:
网站A使用Weibo的接口,使Weibo的用户不用在网站A上注册新用户即可登陆网站A,并使用网站A的相关功能.
开发指导.
1.安装 oauth2
Gemfile
gem 'oauth2', :git => 'https://github.com/oldfritter/oauth2'
申请key和secret在此就不赘述了
2.业务代码范例
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,
所以就有了上面两行)
获取授权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'}

1169

被折叠的 条评论
为什么被折叠?



