FatSecret OmniAuth

A Quick Intro - Part 2

In part one we installed and configured the FatSecret gem. In part two,
we’ll explore ways to call the FatSecret API.

Also, you’ll need to make sure you’re using the latest version of the
fatsecret-omniauth gem. The current version at the time of this blog post is 0.0.2.

The Apis Controller

We created the ApiTokens controller in Part One for obtaining and saving
FatSecret auth tokens. Now, we need a controller to handle api
requests. Open your app folder in a console window and type the following:

Create a new route for the apis#fatsecret method above all other routes in config/routes.rb:

post"/fatsecret",to: "apis#fatsecret"

The apis#fatscret method provides a way to make any FatSecret API request.
You only need to include your FatSecret query in a params hash,
and send this to api#fatsecret. See the FatSecret API method docs
for details on what parameters are required for each FatSecret API method.

To test this, let’s create a form to use the foods.search method without user authentication:

On your user screen click on ‘Add FatSecret’and authenticate with your FatSecret account
(as described in Part 1 of this post).

If successful, your FatSecret user auth_token and auth_secret will be saved in you app
user’s api_tokens database, and you should see the ‘Get FatSecret Profile’ link as pictured below.

When you click the ‘Get FatSecret Profile’ link, an authenticated API call to FatSecret will return
your FatSecret user profile.

That’s it. We made authenticated and unauthenticated calls to the FatSecret API. We used a form for one and a link for the other.
Going forward, all you need to do is read the FatSecret API docs and make sure to include all required
FatSecret method parameters in the params hash you send to the apis#fatsecret method.

Bonus Points

Here’s an example of posting data to a user’s FatSecret account via an authenticated api call.
This follows the same pattern as before. Read the API docs and include the required parameters in the params hash.

Add this form below the ‘Get FatSecret Profile’ link in app/views/users/show.html.erb: