`
南京老油条
  • 浏览: 55694 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

简单搞定第三方用户方接入

 
阅读更多

本文介绍通用的使用第三方接入的方法,可以类似做出接入其它网站(如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'}

 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics