如何修复 795:'FORBIDDEN' 处的意外令牌? Rails 4.2.3 和 HTTParty + GET

How to fix 795: unexpected token at 'FORBIDDEN'? Rails 4.2.3 and HTTParty + GET

我有以下 class 和控制器

 class Recipe
  include HTTParty

  hostport = ENV["FOOD2FORK_SERVER_AND_PORT"] || "www.food2fork.com"
  base_uri "http://#{hostport}/api"
  default_params key: ENV["FOOD2FORK_KEY"]
  format :json

  def self.for (keyword)
    request = get("/search", query: { q: keyword })
    begin
      request.inspect
    rescue => e
      puts e.inspect
    end
    return request.parsed_response["recipes"]
  end
end

 class RecipesController < ApplicationController
  def index
    @search_term = params[:search] || "chocolate"
    @recipes_found = Recipe.for(@search_term)
  end
end

在控制台中工作正常


JSON::RecipesController 中的 ParserError#index

How to fix 795: unexpected token at 'FORBIDDEN'?

这意味着在 ENV["FOOD2FORK_KEY"] 中给出的密钥是错误的,你应该使用 puts 来检查它是否不是 nil。

我知道这有点老了,但是如果你正在学习这门课程,Wiki 中有一些错误的语法

`echo 'export FOOD2FORK_KEY = "Your API Key"' >> ~/.profile`

应该是

`echo 'export FOOD2FORK_KEY="Your API Key"' >> ~/.profile`

您需要编辑您的 ~/.profile 并删除那些设置的 ENV Vars