简体   繁体   English

在Rails中将外部API与JSON数据一起使用的入门

[英]Getting started with using an external API with JSON data in Rails

I am new to Rails and am trying to figure out where I put my API Token for the external API I am using (one of Google's). 我是Rails的新手,正在尝试弄清楚将我的API令牌放在我正在使用的外部API(Google的其中一个)中的位置。 I have worked with APIs in the past with Sinatra, but the directory structure of Rails has thrown me off with where I need to place it so I can access it in the controller. 过去我曾与Sinatra一起使用过API,但是Rails的目录结构使我无法放置它,因此我可以在控制器中对其进行访问。 After I place my token somewhere, I plan on creating a create method in the controller and parsing the json data there so I can access it in my corresponding view. 将令牌放置在某处之后,我计划在控制器中创建一个create方法并在其中解析json数据,以便可以在相应的视图中访问它。 If someone could help guide me in the right direction as to where I put the token so I can access it (best practices), and if I'm on the right track to use the token in a method in the controller so I can access it in a view. 如果有人可以在正确的方向上指导我放置令牌的位置,以便我可以访问它(最佳实践),并且我走在正确的轨道上可以在控制器的方法中使用令牌,则可以访问它在视图中。

I know this question might be generic but from what I have Googled, many people new to Rails might benefit from this as to where to put things. 我知道这个问题可能很笼统,但是从我的Google搜索中可以发现,Rails的许多新手都可以从中受益。

I would suggest loading your API keys via a rails initializer. 我建议通过Rails初始化程序加载您的API密钥。 The rails initializers exist in config/initializers and are plain ruby scripts that run after the servers starts up. Rails初始化程序存在于config / initializers中,并且是在服务器启动后运行的普通ruby脚本。 Here you can do things like load configuration files etc. For example, config/initializers/google_oauth.rb could contain some plain ruby code to load up a config/.yml file holding your API credentials for non-production environments. 在这里,您可以执行诸如加载配置文件之类的操作。例如,config / initializers / google_oauth.rb可能包含一些简单的红宝石代码,以加载config / .yml文件,其中包含用于非生产环境的API凭据。

In non-production environments, you could load the API tokens from a yml file and in production you could utilize something like Figaro for Heroku or Dotenv for other environments (AWS, DigitalOcean, etc). 在非生产环境中,您可以从yml文件加载API令牌,而在生产环境中,可以将Figaro用于Heroku,将Dotenv用于其他环境(AWS,DigitalOcean等)。

The important thing to ensure is that the local configuration file and your API token stay out of version control so as to avoid compromising your token and the security of your application. 确保重要的事情是本地配置文件和您的API令牌应不受版本控制,以免损害令牌和应用程序的安全性。

You can add your API Tokens under config/initializers . 您可以在config/initializers下添加API令牌。 Although, you'll probably have a gem or directions from corresponding API docs telling you what the best way is to implement them. 虽然,您可能会从相应的API文档中看到一些gem或说明,告诉您实现它们的最佳方法是什么。 But if you were implement them via an initializer, it would be something like this - 但是,如果通过初始化程序实现它们,则将是这样的-

GoogleApi.config do |config|
     config.client_id = "<Your Google API Client Id>"
     config.client_secret = "<Your Application Secret>"
     config.application_name = "<Your Application Name>"
end

And then you'll be able to use GoogleApi in your controllers. 然后,您将可以在控制器中使用GoogleApi

A good example is this guide from heroku to access AWS 一个很好的例子是来自heroku 本指南来访问AWS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM