app/controllers/api/json/imports_controller.rb
def validate_service_oauth_code
    success = false

    oauth = current_user.oauths.select(params[:id])
    raise CartoDB::Datasources::AuthError.new("OAuth already set for service #{params[:id]}") unless oauth.nil?

    datasource = CartoDB::Datasources::DatasourcesFactory.get_datasource(params[:id], current_user)
    raise CartoDB::Datasources::AuthError.new("Couldn't fetch datasource for service #{params[:id]}") if datasource.nil?
    unless datasource.kind_of? CartoDB::Datasources::BaseOAuth
      raise CartoDB::Datasources::InvalidServiceError.new("Datasource #{params[:id]} does not support OAuth")
    end

    raise "Missing oauth verification code for service #{params[:id]}" unless params[:code].present?

    begin
      auth_token = datasource.validate_auth_code(params[:code])
      current_user.oauths.add(params[:id],auth_token)
      success = true
    rescue CartoDB::Datasources::AuthError
      CartoDB::Logger.info('Error: validate_service_oauth_code', \
        "Couldn't add oauth from service #{params[:id]} for user #{current_user.username}")
    end

    render_jsonp({ success: success })
  rescue CartoDB::Datasources::TokenExpiredOrInvalidError => ex
    current_user.oauths.remove(ex.service_name)
    render_jsonp({ errors: { imports: INVALID_TOKEN_MESSAGE } }, 401)
  rescue => ex
    CartoDB::Logger.info('Error: validate_service_oauth_code', "#{ex.message} #{ex.backtrace.inspect}")
    render_jsonp({ errors: { imports: ex.message } }, 400)
  end
# Only of use if service is set to work in authorization code mode. Ignore for callback-based oauths
Only of use if service is set to work in authorization code mode. Ignore for callback-based oauths
Only of use if service is set to work in authorization code mode . Ignore for callback - based oauths
def validate_service_oauth_code
    success = false
    oauth = current_user.oauths.select(params[:id])
    raise CartoDB::Datasources::AuthError.new("OAuth already set for service #{params[:id]}") unless oauth.nil?
    datasource = CartoDB::Datasources::DatasourcesFactory.get_datasource(params[:id], current_user)
    raise CartoDB::Datasources::AuthError.new("Couldn't fetch datasource for service #{params[:id]}") if datasource.nil?
    unless datasource.kind_of? CartoDB::Datasources::BaseOAuth
      raise CartoDB::Datasources::InvalidServiceError.new("Datasource #{params[:id]} does not support OAuth")
    end
    raise "Missing oauth verification code for service #{params[:id]}" unless params[:code].present?
    begin
      auth_token = datasource.validate_auth_code(params[:code])
      current_user.oauths.add(params[:id],auth_token)
      success = true
    rescue CartoDB::Datasources::AuthError
      CartoDB::Logger.info('Error: validate_service_oauth_code', \
        "Couldn't add oauth from service #{params[:id]} for user #{current_user.username}")
    end
    render_jsonp({ success: success })
  rescue CartoDB::Datasources::TokenExpiredOrInvalidError => ex
    current_user.oauths.remove(ex.service_name)
    render_jsonp({ errors: { imports: INVALID_TOKEN_MESSAGE } }, 401)
  rescue => ex
    CartoDB::Logger.info('Error: validate_service_oauth_code', "#{ex.message} #{ex.backtrace.inspect}")
    render_jsonp({ errors: { imports: ex.message } }, 400)
  end
def validate_service_oauth_code success = false oauth = current_user . oauths . select ( params [ :id ] ) raise CartoDB :: Datasources :: AuthError . new ( "OAuth already set for service #{params[:id]}" ) unless oauth . nil? datasource = CartoDB :: Datasources :: DatasourcesFactory . get_datasource ( params [ :id ] , current_user ) raise CartoDB :: Datasources :: AuthError . new ( "Couldn't fetch datasource for service #{params[:id]}" ) if datasource . nil? unless datasource . kind_of? CartoDB :: Datasources :: BaseOAuth raise CartoDB :: Datasources :: InvalidServiceError . new ( "Datasource #{params[:id]} does not support OAuth" ) end raise "Missing oauth verification code for service #{params[:id]}" unless params [ :code ] . present? begin auth_token = datasource . validate_auth_code ( params [ :code ] ) current_user . oauths . add ( params [ :id ] , auth_token ) success = true rescue CartoDB :: Datasources :: AuthError CartoDB :: Logger . info ( 'Error: validate_service_oauth_code' , "Couldn't add oauth from service #{params[:id]} for user #{current_user.username}" ) end render_jsonp ( { success : success } ) rescue CartoDB :: Datasources :: TokenExpiredOrInvalidError => ex current_user . oauths . remove ( ex . service_name ) render_jsonp ( { errors : { imports : INVALID_TOKEN_MESSAGE } } , 401 ) rescue => ex CartoDB :: Logger . info ( 'Error: validate_service_oauth_code' , "#{ex.message} #{ex.backtrace.inspect}" ) render_jsonp ( { errors : { imports : ex . message } } , 400 ) end
Only of use if service is set to work in authorization code mode.
Only of use if service is set to work in authorization code mode .