推送到heroku后出现500错误-调试

时间:2018-10-25 01:21:15

标签: ruby-on-rails heroku migration open-uri

生产中出现openuri问题。以下代码在开发中可用,但在生产中会出现未经授权的错误。我解决此问题的唯一方法是,如果我将api键直接放在url中,显然我不想这样做。任何想法为何我当前的代码不起作用?

 api_key = ENV["NEWS_API"]
 url = "https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=#{api_key}"
 article_serialized = open(url).read
@articles = JSON.parse(article_serialized)

2018-10-25T01:55:47.184856+00:00 app[web.1]: F, [2018-10-25T01:55:47.184804 #4] FATAL -- : [40a7ee38-ef6e-4622-ad86-edb34d7eeccf] OpenURI::HTTPError (401 Unauthorized):

Running rails db:migrate on ⬢ twittter-clone... up, run.6483 (Free)
D, [2018-10-25T01:12:44.387465 #4] DEBUG -- :    (0.9ms)  SELECT pg_try_advisory_lock(2661719123600558280)
D, [2018-10-25T01:12:44.433095 #4] DEBUG -- :    (2.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
D, [2018-10-25T01:12:44.456835 #4] DEBUG -- :   ActiveRecord::InternalMetadata Load (3.8ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", "environment"], ["LIMIT", 1]]
D, [2018-10-25T01:12:44.480937 #4] DEBUG -- :    (5.3ms)  BEGIN
D, [2018-10-25T01:12:44.485057 #4] DEBUG -- :    (1.7ms)  COMMIT
D, [2018-10-25T01:12:44.492822 #4] DEBUG -- :    (7.1ms)  SELECT pg_advisory_unlock(2661719123600558280)

1 个答案:

答案 0 :(得分:0)

OpenURI返回401错误,我的猜测是未设置ENV['NEWS_API']。您可以在终端中运行heroku config或在Web界面上查找它:Settings-> Reveal Config Vars