Search code examples
ruby-on-railsrubysalesforceintegrationdatabasedotcom-gem

how to get data salesforce accounts in ruby app according to user login?


I am using databasedotcom gem. I am able to get data for 1 account only by specifying Client_id, client_secret, username and password in config/database.yml file. but i want to get data according to user login. 1st user login with salesforce he will get data from his salesforce account. same for 2nd 3rd and 4th. Any suggestion will be appreciated.

Example code is below:-

database.yml:-

host: login.salesforce.com client_id: the Consumer Key from Salesforce

client_secret: the Consumer Secret from Salesforce

username: username

password: password+securitytoken

debugging: true

sfdc_controller:-

class Api::V1::SfdcsController < ApplicationController
include Databasedotcom::Rails::Controller
def getSfdcauthentication
username = params[:username]
password = params[:password]
client_id = params[:client_id]
client_secret = params[:client_secret]

client = Databasedotcom::Client.new :client_id => client_id, :client_secret => client_secret
begin
  oauth_token = client.authenticate :username => username, :password => password  #=> "the-oauth-token"
rescue =>e
  oauth_token = false
end

if oauth_token
  contact_class = client.materialize("Contact")
  @sf_contacts = Contact.all
respond_with(@sf_contacts) do |format|
  format.json { render :json => @sf_contacts.as_json }
end
else
  render json: {status:200,message:"Authentication failed"}
end
end
end

Solution

  • I found an another gem omniauth-salescforce and it worked well for me. I found very good guide from here:- http://geekymartian.com/articles/ruby-on-rails-4-salesforce-oauth-implementation/

    and I found the sample code example from here :- https://github.com/takahiro-yonei/OmniAuth-Salesforce-Sample