function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
betterbhushanbetterbhushan 

access token not returning all parameters via oauth2 in Rails 3.1.1

I am using oauth2 and databaseforcom gem to connect with Salesforce.com and extract user's data. But my access_token is not returning all the parameters it is supposed to retrun viz. instance_url. Below is my authentication code.

class SfdcController < ApplicationController
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
  #include Databasedotcom::Rails::Controller
 # $sfdc_app_config=YAML.load_file("#{Rails.root.to_s}/config/databasedotcom.yml")


  def oauth_client
    consumer_key = ''
    consumer_secret = ''
    OAuth2::Client.new(consumer_key, consumer_secret, {:site => 'https://login.salesforce.com', :authorize_path => '/services/oauth2/authorize', :access_token_path => '/services/oauth2/token', :ssl=>{:verify => false }})
  end

  #
  #Creates the uri which will be the redirect path
  #
  def oauth_redirect_uri
    uri = URI.parse(request.url)
    uri.path = '/sfdc/oauth_callback'
    uri.query = nil
    uri="http://localhost:3000/sfdc/oauth_callback"
    uri.to_s
  end
  #
  #This is the action which will fetch the access token.
  #
  def oauth_connect
    uri = URI.parse(request.url)
    consumer_key = ''
    redirect_to oauth_client.web_server.authorize_url(
    :redirect_uri => oauth_redirect_uri,
    :client_id => consumer_key,
    :response_type => 'code'
    )
  end

  #
  #This is the action which will handle the callback once the authorization is done
  #
  def oauth_callback
    access_token = oauth_client.web_server.get_access_token(params[:code], :redirect_uri => oauth_redirect_uri, :grant_type => 'authorization_code')
    client = Databasedotcom::Client.new("#{Rails.root}/config/databasedotcom.yml")
    client.authenticate :token => access_token.token, :instance_url => "http://ap1.salesforce.com"
    client.materialize('Account')
    render :text => access_token.token
  end
end

 

and access_token it gives is 

 

<OAuth2::AccessToken:0x12b96f830 @refresh_token="5Aep8617VFpoP.M.4tK4KlWuvvwufeW5s3R_RMldf5nRDDnuQ1gZ08F.GQXQaIveDsyLrv4W5fShQ==", @token="00D90000000bfa4!AQMAQMDfipmZ2129JCGNhXecqOnYAbeImhSLTA1j5rWxB9NX2esGlgQkIHVfCyNMQe0ETJQJb16m7v74sgjX5ErUZJzOUm0h", @client=#<OAuth2::Client:0x12b85a120 @options={:authorize_path=>"/services/oauth2/authorize", :ssl=>{:verify=>false}, :access_token_path=>"/services/oauth2/token"}, @site="https://login.salesforce.com", @connection=#<Faraday::Connection:0x12b85a0d0 @options={}, @path_prefix="/", @scheme="https", @host="login.salesforce.com", @headers={}, @port=nil, @parallel_manager=nil, @builder=#<Faraday::Builder:0x12b859c70 @handlers=[#<Proc:0x0000000102680fd0@/Users/bhushan/.rvm/gems/ruby-1.8.7-p352@companyplus/gems/faraday-0.4.6/lib/faraday/builder.rb:17>, #<Proc:0x000000010267ee60@/Users/bhushan/.rvm/gems/ruby-1.8.7-p352@companyplus/gems/faraday-0.4.6/lib/faraday/builder.rb:55>]>, @ssl={}, @params={}>, @secret="7223836429183804591", @id="3MVG9Y6d_Btp4xp5____________________________________Pft9QvURaQN", @json=nil>, @expires_in=nil>

 

when I do

 

access_token["instance_url"]

 

I get 

 

undefined method `[]' for #<OAuth2::AccessToken:0x12b99be80>

 

 

what is wrong?

Navatar_DbSupNavatar_DbSup

Hi,

Try the below

 

<OAuth2::AccessToken:00D90000000bfa4!AQMAQMDfipmZ2129JCGNhXecqOnYAbeImhSLTA1j5rWxB9NX2esGlgQkIHVfCyNMQe0ETJQJb16m7v74sgjX5ErUZJzOUm0h>

 

instead of

 "<OAuth2::AccessToken:0x12b99be80>".

 

According to http response access token should be "00D90000000bfa4!AQMAQMDfipmZ2129JCGNhXecqOnYAbeImhSLTA1j5rWxB9NX2esGlgQkIHVfCyNMQe0ETJQJb16m7v74sgjX5ErUZJzOUm0h not 0x12b96f830"

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.