首页 > 解决方案 > Xeroizer 合作伙伴应用程序:更新访问令牌不起作用 Rails 4.2.0 Ruby 2.2.9

问题描述

我正在使用 ruby​​ gem 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

特别感谢!提前。

标签: rubyruby-on-rails-4xero-apixeroizer

解决方案


推荐阅读