尝试使用Ruby连接到Bigcommerce API时出现错误
问题描述:
我是Ruby新手和BC API的新手,所以可能会有些显而易见的东西丢失。当我正在用我的店铺遗留API凭证更换适当的细节如下代码:尝试使用Ruby连接到Bigcommerce API时出现错误
require 'bigcommerce'
Bigcommerce.configure do |config|
config.auth = 'legacy'
# You will get this url when registering for an API key
config.url = ENV['BC_API_ENDPOINT_LEGACY']
config.username = ENV['BC_USERNAME']
config.api_key = ENV['BC_API_KEY']
end
puts Bigcommerce::System.time
我得到以下错误:
.../lib/ruby/2.3.0/net/http.rb:882:in `rescue in block in connect': Failed to open TCP connection to :80 (Connection refused - connect(2) for nil port 80) (Faraday::ConnectionFailed)
我明白任何指针。
答
tldr:你的环境变量没有真正被设置:对
听起来未实际设置环境变量。 我这样说是因为错误Failed to open TCP connection to :80
,没有列出任何URL,但只有端口。
要确认,在您的代码中,您是否可以简单地运行: puts ENV['BC_API_ENDPOINT_LEGACY']
,并查看是否打印了任何内容?
另一种方法是仅对您的凭证进行硬编码,而不依赖于环境变量。
Bigcommerce.configure do |config|
config.auth = 'legacy'
config.url = 'https://url.com'
config.username = 'api_username'
config.api_key = 'api_key'
end
仅供参考,你的方式设置环境变量通常会(在命令行):export BC_API_ENDPOINT_LEGACY="https://url.com"
并确认它是由echo $BC_API_ENDPOINT_LEGACY
设置您可以为其他选项查看this link为您的RoR应用程序设置环境变量。