Skip to content

Instantly share code, notes, and snippets.

@oseifrimpong
Created August 4, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oseifrimpong/e241c7c901138d5758c8 to your computer and use it in GitHub Desktop.
Save oseifrimpong/e241c7c901138d5758c8 to your computer and use it in GitHub Desktop.
require 'adwords_api'
class LoginController < ApplicationController
skip_before_filter :authenticate
GOOGLE_LOGOUT_URL = 'https://www.google.com/accounts/Logout'
def prompt()
api = get_adwords_api()
if session[:token]
redirect_to home_index_path
else
begin
token = api.authorize({:oauth2_callback => login_callback_url})
rescue AdsCommon::Errors::OAuth2VerificationRequired => e
@login_url = e.oauth_url
end
end
end
def callback()
api = get_adwords_api()
begin
session[:token] = api.authorize(
{
:oauth2_callback => login_callback_url,
:oauth2_verification_code => params[:code]
}
)
flash.notice = 'Authorized successfully'
redirect_to home_index_path
rescue AdsCommon::Errors::OAuth2VerificationRequired => e
flash.alert = 'Authorization failed'
redirect_to login_prompt_path
end
end
def logout()
[:selected_account, :token].each {|key| session.delete(key)}
redirect_to GOOGLE_LOGOUT_URL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment