mirror of
https://github.com/wahyd4/Libr.git
synced 2026-08-09 04:46:07 +10:00
add qq login
This commit is contained in:
@@ -11,4 +11,9 @@ Build Status
|
||||
|
||||
export CLIENT_ID='0858f96ac849895e190aec4058dc9c1a'
|
||||
export CLIENT_SECRET='0c363bc78dbacb3f'
|
||||
export REDIRECT_URI='http://localhost:3000/douban_callback'
|
||||
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'
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 7.2 KiB |
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user