Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created May 9, 2013 07:49
Show Gist options
  • Select an option

  • Save NuckChorris/5546133 to your computer and use it in GitHub Desktop.

Select an option

Save NuckChorris/5546133 to your computer and use it in GitHub Desktop.
require 'rest_client'
require 'uri'
class VotingTokensController < ApplicationController
# TODO: Refactor this hideous beasts.
# Seriously, I really deserve to die for the awfulness herein contained.
def oauth_predirect
ApplicationController
# redirect_to "https://www.deviantart.com/oauth2/draft10/authorize?response_type=code&client_id
# =394&redirect_uri=http://test.emotecloud.net:3000/oauth/redirect"
redirect_uri = URI "#{Rails.application.config.oauth['oauth_root_uri']}/authorize"
redirect_uri.query = {
:redirect_uri => "http://#{request.host_with_port}/oauth/redirect",
:client_id => Rails.application.config.oauth['client_id'],
:response_type => 'code'
}.to_query
redirect_to redirect_uri.to_s
end
def oauth_redirect
if params[:error]
render 'error/oauth'
return
end
token_response = JSON.parse(RestClient.post("#{Rails.application.config.oauth['oauth_root_uri']}/token", {
:client_id => Rails.application.config.oauth['client_id'],
:client_secret => Rails.application.config.oauth['client_secret'],
:code => params[:code],
:grant_type => 'authorization_code'
}))
p "#{Rails.application.config.oauth['api_root_uri']}/user/whoami?access_token=" + token_response['access_token']
username = JSON.parse(RestClient.post("#{Rails.application.config.oauth['api_root_uri']}/user/whoami", {
:access_token => token_response['access_token']
}))['username']
user = User.where(:username => username).first_or_create({
:username => username,
:access_token => token_response['access_token'],
:reset_token => token_response['reset_token'],
:token_expiry => Time.new + token_response['expires_in']
})
@token = VotingToken.create :user => user
end
def oauth_identify
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment