gpt4 book ai didi

ruby-on-rails - 获取 "RuntimeError: SSL Session wasn' t 初始化”错误 rails

转载 作者:行者123 更新时间:2023-12-04 02:24:25 26 4
gpt4 key购买 nike

目前,我正在与使用 3-legged OAuth 安全性的 API 进行通信。

对于通信,我每次都需要访问 token 。访问 token 自创建之日起有效期为 1 年。

我已经将它存储在数据库中(AccessToken 类的序列化对象)。

但是在进行 API 调用时出现以下错误。
这是我的基本流程

1) 获取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) 将 access_token 存储在数据库

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) 获取 token 并将其用于调用 API
access_token = AccessTokenUser.find_by(user_id: 1).access_token

access_token.get('/organizations')

在获取组织详细信息时,我收到以下错误
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>'

谁能让我知道我该如何解决这个问题?

最佳答案

一旦您序列化 AccessToken,SSL session 就不会被持久化。对象并将其写入数据库。从数据库中检索访问 token 后,您可能需要重新初始化它。

序列化列通常用于散列和数组,而不是整个类。所以你可能想要存储 access token verifieraccess_token_users表而不是 access_token本身,然后调用:

 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')

关于ruby-on-rails - 获取 "RuntimeError: SSL Session wasn' t 初始化”错误 rails ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com