Skip to content

Instantly share code, notes, and snippets.

@krongk
Last active September 19, 2016 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krongk/22bc15da7bd37ea9f7ad to your computer and use it in GitHub Desktop.
Save krongk/22bc15da7bd37ea9f7ad to your computer and use it in GitHub Desktop.
Omniauth Strategy TQQ 腾讯QQ Oauth2
#C:\Sites\tm_wed\lib\omniauth\strategies\tqq.rb
# Updated at: 2014-09-04
# Author: we@wedxt.com
require "omniauth-oauth2"
module OmniAuth
module Strategies
class Tqq < OmniAuth::Strategies::OAuth2
option :name, 'tqq'
option :client_options, {
:site => "https://graph.qq.com",
:authorize_url => "/oauth2.0/authorize",
:token_method => :get,
:token_url => "/oauth2.0/token",
:token_formatter => lambda {|hash|
hash[:expires_in] = hash['expires_in'].to_i
hash.delete('expires_in')
}
}
option :authorize_params, {
:response_type => "code"
}
option :authorize_options, []
option :token_params, {
:grant_type => "authorization_code",
:parse => :query,
:state => "state"
}
option :token_options, []
uid do
response = access_token.get(
'/oauth2.0/me'
)
data = MultiJson.decode(response.body.gsub(/callback\(/, "").gsub(/\);\n/, "").strip, :symbolize_keys => true)
@user_id = data[:openid]
end
info do
{
:nickname => raw_info['nickname'],
:avatar => raw_info['figureurl_2'],
:gender => raw_info['gender']
}
end
extra do
{
:raw_info => raw_info
}
end
def raw_info
response = access_token.get(
'/user/get_user_info', {:params => {:openid => @user_id, :oauth_consumer_key => access_token.client.id, :format => :json}, :parse => :json}
).parsed
end
def build_access_token
verifier = request.params['code']
client.auth_code.get_token(
verifier,
{:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)),
{:mode => :query, :param_name => 'access_token'}
)
end
end
end
end
OmniAuth.config.add_camelization 'tqq', 'Tqq'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment