Currently, I am communicating with the API that uses 3-legged OAuth security.
For communication, I need access token each time. Access Token is valid for 1 year from the creation date.
I have stored it in the database (serialized object of AccessToken class).
But while making API call I am getting following error. Here is my basic flow
1) Get the access_token
consumer = OAuth::Consumer.new API_KEY,
API_SECRET,
{
site: 'https://example.com',
header: { Accept: 'application/nd.v3+json' },
http_method: :get,
request_token_url: @request_token_uri,
access_token_url: @access_token_uri,
authorize_url: @authorizerequest_token_uri
}
puts '***** Fetching request token *****'
request_token = consumer.get_request_token({}, 'oob')
puts "Request Token received - #{request_token.token}"
puts
puts 'Goto to url mentioned below to authorize. Paste the access token verifier'
puts request_token.authorize_url
verifier = gets.chomp
puts
puts
puts '***** Fetching access token *****'
access_token = request_token.get_access_token(oauth_verifier: verifier)
2) Store the access_token in database
access_token_user.rb
class AccessTokenUser < ActiveRecord::Base
belongs_to :user
serialize :access_token
end
atu = AccessTokenUser.create(user_id: 1, access_token: access_token)
3) Fetch the token and us it for calling API
access_token = AccessTokenUser.find_by(user_id: 1).access_token
access_token.get('/organizations')
While fetching the organizations detail I am getting following error
RuntimeError: SSL Session wasn't initialized.
from /home/test/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/net/http.rb:916:in `time'
from /home/test/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/net/http.rb:916:in `connect'
from /home/test/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /home/test/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/net/http.rb:851:in `start'
from /home/test/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/net/http.rb:1373:in `request'
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/oauth-0.4.7/lib/oauth/consumer.rb:161:in `request'
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/oauth-0.4.7/lib/oauth/tokens/consumer_token.rb:25:in `request'
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/oauth-0.4.7/lib/oauth/tokens/access_token.rb:12:in `request'
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/oauth-0.4.7/lib/oauth/tokens/access_token.rb:27:in `get'
from (irb):18
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.0.13/lib/rails/commands/console.rb:90:in `start'
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.0.13/lib/rails/commands/console.rb:9:in `start'
from /home/test/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.0.13/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Can anyone let me know how can I fix that issue?
The SSL session is not being persisted once you serialize the AccessToken
object and write it to your database. You may need to re-initialize the access token after retrieving it from the database.
Serialized columns are usually used for hashes and arrays, not entire classes. So you may want to store the access token verifier
in the access_token_users
table instead of the access_token
itself, then call:
verifier = AccessTokenUser.find_by(user_id: 1).verifier
request_token = consumer.get_request_token({}, 'oob')
access_token = request_token.get_access_token(oauth_verifier: verifier)
access_token.get('/organizations')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With