Curling and Parsing URL from Fitbit API

Viewed 332

I'm relatively new to Rails web development and coding in general. I'm currently developing a web application that should retrieve data from my Fitbit account. This is done through the Fibit API and OAuth2.0.

Essentially, here's my problem. As shown on the Fitbit tutorial website, https://dev.fitbit.com/apps/oauthinteractivetutorial, I need to get authorization from the Fitbit user. I've tried to do this using the following correct URL

<%= link_to "Sign in with Fitbit", "https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=228BT5&redirect_uri=http%3A%2F%2Fwww.builtbyburton.com%2Foauth2%2Fcallback&scope=activity%20location%20profile%20sleep&expires_in=604800" %>

When this is called, the user is redirected to the following URL as expected:

http://www.builtbyburton.com/oauth2/callback?code=1093d4dc25f0c61acbbf8128a9247b0efd448c35#_=_

Currently, I need to capture the segment of the URL from the code= to #_=_ This segment then needs to be parsed. I've tried to do it using an embedded curl as shown:

<pre class="embedcurl">curl -X POST -i -H 'Authorization: Basic MjI4QlQ1OmU4OWFkZWJmYTUzNTU1MGMyNGUyNjdhOWRjM2MxNzIy' -H 'Content-Type: application/x-www-form-urlencoded' -d "clientId=228BT5" -d "grant_type=authorization_code" -d "redirect_uri=http%3A%2F%2Fwww.builtbyburton.com%2Foauth2%2Fcallback" -d "code=1093d4dc25f0c61acbbf8128a9247b0efd448c35" https://api.fitbit.com/oauth2/token</pre>

From there, I need to parse the response which will allow me to make requests. Overall, I'm just not sure how to get from the authorization link to making a request using an OAuth2.0 Access Token. Thanks for the help!

1 Answers
Related