How to keep alive box token?

283 views Asked by At

I have a rails app running on heroku from where I need to create Box.com! folder on an item creation callback using gem for the Box Content API!

The Standard OAuth 2.0 (User Authentication) of box api providing token which last an hour. I need the token alive for all the time so that the app can create box folder anytime from the app.

Recently, implemented box webhook feature as well.

I have tried couple of ways below but nothing help:

  1. Token refresh callback as suggested the boxr gem!
    token_refresh_callback = lambda {|access, refresh, identifier|
      Setting.box_access_token     = access
      Setting.box_refresh_token    = refresh
    }

    @client = Boxr::Client.new(
        Setting.box_access_token,
        refresh_token: Setting.box_refresh_token,
        client_id:     Setting.box_client_id,
        client_secret: Setting.box_client_secret, &token_refresh_callback
    )
  1. Called a method before initialisation to update token
unless (Time.now.to_i >= Setting.box_token_expires_in.to_i - 300)
      token                        = Boxr::refresh_tokens(Setting.box_refresh_token, client_id: Setting.box_client_id, client_secret: Setting.box_client_secret)
      Setting.box_access_token     = token.access_token
      Setting.box_refresh_token    = token.refresh_token
      Setting.box_token_expires_in = Time.now.to_i + token.expires_in.to_i
    end
  1. Used scheduler which basically call a method to perform what did in previous step.

Step 2 were working before but sometimes have got refresh token expired exception. suddenly it does not work, require to manually reset token in every hour. Not sure but it might started after implementing box webhook feature.

Would be happy to have suggestion/solution to keep the token alive...

0

There are 0 answers