如何解决795:意外令牌在' FORBIDDEN'? Rails 4.2.3和HTTParty + GET

时间:2016-02-03 15:31:19

标签: ruby-on-rails httparty

我有以下类和控制器

 [HttpPost]
    public ActionResult OracleVerification(string company, string location, string product, string department)
    {

        List<OracleStringViewModel> oracleStringList = OracleStringRepository.GetOracleStrings(company, location, product, department);


        return View(oracleStringList.ToList());

    }
 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

在控制台中正常工作 Rails Console output

RecipesController中的JSON :: ParserError #index

Browser output

2 个答案:

答案 0 :(得分:0)

  

如何修复795:意外令牌在&#39; FORBIDDEN&#39;?

这意味着ENV [&#34; FOOD2FORK_KEY&#34;]中给出的密钥是错误的,您应该使用puts来检查是否为零。

答案 1 :(得分:0)

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

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

应该是

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

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