diff --git a/README.md b/README.md index 80ec22c..c822c5e 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,9 @@ Build Status export CLIENT_ID='0858f96ac849895e190aec4058dc9c1a' export CLIENT_SECRET='0c363bc78dbacb3f' - export REDIRECT_URI='http://localhost:3000/douban_callback' \ No newline at end of file + export REDIRECT_URI='http://localhost:3000/douban_callback' + + + export QQ_CLIENT_ID='100375105' + export QQ_CLIENT_SECRET='773a9c80d43b0881e4344fd453d5dca4' + export QQ_REDIRECT_URI='http://libr.herokuapp.com/qq_callback' \ No newline at end of file diff --git a/app/assets/images/qq_login.png b/app/assets/images/qq_login.png new file mode 100644 index 0000000..ebca592 Binary files /dev/null and b/app/assets/images/qq_login.png differ diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 6d5bf4e..82b5e5c 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,9 @@ require "douban_auth" +require "qq_auth" class HomeController < ApplicationController include Douban_Auth + include QQAuth before_filter :current_user def index @@ -15,5 +17,11 @@ class HomeController < ApplicationController sign_in(user_info['name'],user_info['avatar']) redirect_to '/' end - + def qq_callback + token = qq_auth_token params[:code] + qq_openid = fetch_qq_open_id token + user_info = fetch_qq_user_info token,qq_openid + sign_in(user_info['nickname'],user_info['figureurl_1']) + redirect_to '/' + end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 0f14111..189dfbe 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,8 +1,10 @@ require "douban_auth" +require "qq_auth" class UserController < ApplicationController before_filter :current_user include Douban_Auth + include QQAuth def login render :layout => false @@ -11,7 +13,12 @@ class UserController < ApplicationController def auth_douban url = auth_url.to_s redirect_to url - end + end + + def auth_qq + url = qq_auth_url.to_s + redirect_to url + end def logout session[:name] = nil diff --git a/app/views/user/login.html.slim b/app/views/user/login.html.slim index e8613b6..505665b 100644 --- a/app/views/user/login.html.slim +++ b/app/views/user/login.html.slim @@ -17,5 +17,5 @@ html lang="en" a href="/login/douban" img src="/assets/douban.png" p - a href="" - img src="/assets/weibo.png" + a href="/login/qq" + img src="/assets/qq_login.png" diff --git a/config/routes.rb b/config/routes.rb index 5a1160d..9546f99 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,7 +11,9 @@ Libr::Application.routes.draw do get '/login' => 'user#login' get '/logout' => 'user#logout' get '/login/douban' => 'user#auth_douban' + get '/login/qq' => 'user#auth_qq' get '/douban_callback' =>'home#douban_callback' + get '/qq_callback'=>'home#qq_callback' get '/users/:id' => 'user#view' # The priority is based upon order of creation: diff --git a/lib/qq_auth.rb b/lib/qq_auth.rb new file mode 100644 index 0000000..33d90a6 --- /dev/null +++ b/lib/qq_auth.rb @@ -0,0 +1,55 @@ +require 'net/http' +require 'net/https' + +module QQAuth + def qq_auth_url + response_type = 'code' + scope = 'get_user_info' + + url = 'https://graph.qq.com/oauth2.0/authorize?client_id=' + ::ENV['QQ_CLIENT_ID'] + + '&redirect_uri=' + ::ENV['QQ_REDIRECT_URI'] + + '&response_type=' + response_type + + '&scope=' + scope + "&state=libr_test" + + end + + def qq_auth_token(code) + http = Net::HTTP.new('graph.qq.com', 443) + http.use_ssl = true + path ='/oauth2.0/token?' + + '&client_id='+ ::ENV['QQ_CLIENT_ID']+ + '&client_secret='+ ::ENV['QQ_CLIENT_SECRET']+ + '&redirect_uri='+::ENV['QQ_REDIRECT_URI']+ + '&grant_type=authorization_code&code='+code + + response = http.get(path, nil) + response = JSON.parse response.body + response['access_token'] + + end + + def fetch_qq_open_id(access_token) + http = Net::HTTP.new('graph.qq.com', 443) + http.use_ssl = true + path ='/oauth2.0/me?' + + '&access_token='+access_token + + response = http.get(path, nil) + response = JSON.parse response.body + response['openid'] + end + + def fetch_qq_user_info(access_token, openid) + http = Net::HTTP.new('graph.qq.com', 443) + http.use_ssl = true + path ='/user/get_user_info?' + + '&access_token='+access_token + + '&oauth_consumer_key=' +::ENV['QQ_CLIENT_ID'] + + '&openid=' +openid + response = http.get(path, nil) + response = JSON.parse response.body + puts "response====" +response.to_s + response + + end +end \ No newline at end of file