Skip to main content
Question

400 invalid_grant error once, works fine after that

  • May 21, 2025
  • 3 replies
  • 26 views

Forum|alt.badge.img

 

Hi,

 

I'm building my simple app associated with Box API. It is built on Ruby and Rails and Boxr as Ruby client library for Box Content API. https://github.com/cburnette/boxr

It works pefect on my Mac, but it happens weird behavior on Windows server.

On Windows server, when I call `get_user_token` method, then it make me wait about 10 seconds and it returns 400 error with message "Current date\/time MUST be before the expiration date\/time listed in the 'exp' claim", however I try to call it again, works fine.

Server time is synchronized with NTP server.

Whant happen on Box? How can I fix this?

 

Ruby 2.3.1

Rails 5.0.0.1

Boxr 1.2.0

 

Should I ask at Boxr side?

 

PS C:\Users\foo\baz\foo\current> bundle exec rails console
Loading production environment (Rails 5.0.0.1)
irb(main):001:0>   def private_key
irb(main):002:1>     OpenSSL::PKey::RSA.new(Settings.box.private_key,
irb(main):003:2*                            Settings.box.private_key_pass_phrase)
irb(main):004:1>   end
=> :private_key
irb(main):005:0>
irb(main):006:0*   def user_token
irb(main):007:1>     Boxr.get_user_token(Settings.box.app_user_id.to_s,
irb(main):008:2*                         private_key: private_key,
irb(main):009:2*                         private_key_password: Settings.box.private_key_pass_phrase,
irb(main):010:2*                         public_key_id: Settings.box.public_key_id,
irb(main):011:2*                         client_id: Settings.box.client_id,
irb(main):012:2*                         client_secret: Settings.box.client_secret)
irb(main):013:1>   end
=> :user_token
irb(main):014:0> tok = user_token
Boxr::BoxrError: 400: {"error":"invalid_grant","error_description":"Current date\/time MUST be before the expiration date\/time listed in the 'exp' claim"}
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/boxr-1.2.0/lib/boxr/auth.rb:90:in `auth_post'
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/boxr-1.2.0/lib/boxr/auth.rb:24:in `get_tokens'
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/boxr-1.2.0/lib/boxr/auth.rb:39:in `get_user_token'
        from (irb):7:in `user_token'
        from (irb):14
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command
!'
        from C:/Users/foo/baz/foo/shared/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `'
        from bin/rails:9:in `require'
        from bin/rails:9:in `'
irb(main):015:0> tok = user_token
=> # token_type="bearer">

 

Thank you

 

3 replies

Forum|alt.badge.img

Did you ever figure out what was going on here?  how did you make it work correctly? I am having a similar problem.  


Forum|alt.badge.img

A comment in https://github.com/cburnette/boxr/issues/30 points out that the exp claim is set to expire in 10 seconds. So if, on first run through the code, it takes more than 10 seconds to initialize everything and make the API call, it will fail.

 

Perhaps the second call is faster because some things are already initialized at that point.

 

Perhaps suggest to the maintainer that the timeout be increased above 10 seconds, or that it be made configurable.


Forum|alt.badge.img

thank you. I think I might have another problem.  But this was helpful!