Xeroizer合作伙伴应用程序:续订访问令牌不起作用Rails 4.2.0 Ruby 2.2.9

时间:2018-07-10 04:33:40

标签: ruby ruby-on-rails-4 xero-api xeroizer

我正在使用红宝石宝石Xeroizer和合作伙伴应用程序https://github.com/waynerobinson/xeroizer,并按照此链接中的说明生成了pem文件。当我更新访问令牌以获取长期连接时,在令牌更新时,设置为“ @expires_at”的所有内容也显示xero端的有效期已延长,但会话在30分钟后断开。新返回的令牌和密钥不相同,我正在保存这些新令牌数据库中的那些。这是下面的代码。

/////////连接

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

/////////回调方法

 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只是在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

//////续订

cronjob更新方法

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

特别感谢!

0 个答案:

没有答案