Xeroizer partner application: renew access token not working Rails 4.2.0 Ruby 2.2.9

157 views Asked by At

I am using a ruby gem Xeroizer and partner application https://github.com/waynerobinson/xeroizer and generated pem file as instructed in this link. when I renew access token to obtain long term connection then upon token renewal everything sets up "@expires_at" also shows extended expiry on xero side but session got disconnected after 30 minutes.New returned token and secret are not same and I am saving these new ones in the database also. Here is my code below.

//////// connecting

def connect_xero_organisation  
  @xeroizer = Xeroizer::PartnerApplication.new(ENV["XERO_GATEWAY_CONSUMER_KEY"], ENV["XERO_GATEWAY_CONSUMER_SECRET"], "#{Rails.root}/config/certs/privatekey.pem")
  request_token = @xeroizer.request_token(oauth_callback: "https://{ENV['HOST_NAME']}/companies/#{@company.slug}/xero")
  session[:request_token]  = request_token.token
  session[:request_secret] = request_token.secret
  redirect_to request_token.authorize_url
end

///////// callback method

 def xero_organisation_detail

   if params[:oauth_verifier].present? 
     @xeroizer = Xeroizer::PartnerApplication.new(ENV["XERO_GATEWAY_CONSUMER_KEY"], ENV["XERO_GATEWAY_CONSUMER_SECRET"], "#{Rails.root}/config/certs/privatekey.pem")
     begin
      @xeroizer.authorize_from_request(session[:request_token], session[:request_secret], oauth_verifier: params[:oauth_verifier])
      session[:xero_auth] = { access_token:   @xeroizer.access_token.token,access_secret:  @xeroizer.access_token.secret }
      session.delete(:request_token)
      session.delete(:request_secret)
      xero_organisation_create_update(@xeroizer)
      redirect_to edit_company_path(@company, xero: 'xero'), notice: "Settings have been saved"
    rescue Exception => e
      redirect_to edit_company_path(@company)   
    end
  else
    @organisation_detail = @company.xero_organisation
    @xero_organisation_accounts_details = @company.xero_organisation_accounts
  end

end

///////// @company is simply setting in before_filter :set_company

def xero_organisation_create_update(xeroizer)

  organisation = xeroizer.Organisation.all(:find => {organisation_id: params[:org]})

  connection_expires_at = xeroizer.client.expires_at
  token = xeroizer.access_token.token
  secret = xeroizer.access_token.secret
  session_handle = xeroizer.session_handle
  organisation_name = organisation.last.name
  code = organisation.last.short_code  

  if @company.xero_organisation.present?
    @company.xero_organisation.update_attributes(name: organisation_name, connection_expires_at: connection_expires_at, short_code: code, xero_token: token, xero_secret: secret, conn_handle: session_handle)
    @organisation_detail = @company.xero_organisation
  else
    @organisation_detail = XeroOrganisation.create(name: organisation_name,connection_expires_at: connection_expires_at,company_id: @company.id,short_code: code, xero_token: token, xero_secret: secret, conn_handle: session_handle)
  end

end

////// renewal

cronjob method for renewal

def self.conn_renewel

  XeroOrganisation.where("connection_expires_at is not NULL").each do |xo|  
    client = Xeroizer::PartnerApplication.new(ENV["XERO_GATEWAY_CONSUMER_KEY"], ENV["XERO_GATEWAY_CONSUMER_SECRET"], "#{Rails.root}/config/certs/privatekey.pem")
    conn = client.renew_access_token(xo.access_token,xo.access_secret,xo.session_handle)
    xo.update_attributes(:xero_token=>conn.first, :xero_secret=>conn.last)  
  end

end

Special thanks! in advance.

0

There are 0 answers