The OAuth Process

Wendy Smoak edited this page Jun 27, 2015 · 4 revisions

The OAuth Process

This page describes how to use fitgem to do authentication/authorization with the OAuth service.

If you've ever done any oauth client programming then the model will appear familiar. Your first step, if haven't already, is to visit and register your application to get your consumer key and consumer secret and set your callback URL, if appropriate for your app. There's more documentation at the site so I won't belabor it here.

Below I've included two sample scripts that use Fitgem::Client to retrieve data. One shows how to do the initial authorization without doing the callback; the other shows how to use saved values from that initial authorization to reconnect with the API and get subsequent information.

require 'fitgem'

consumer_key = 'your-app-consumer-key'
consumer_secret = 'your-app-consumer-secret'

client ={:consumer_key => consumer_key, :consumer_secret => consumer_secret})

request_token = client.request_token
token = request_token.token
secret = request_token.secret

puts "Go to{token} and then enter the verifier code below and hit Enter"
verifier = gets.chomp

access_token = client.authorize(token, secret, { :oauth_verifier => verifier })

puts "Verifier is: "+verifier
puts "Token is:    "+access_token.token
puts "Secret is:   "+access_token.secret

After running this and successfully connecting with verifier string that is displayed by the Fitgem site, you can reconnect using the script below. To do so, take the token and secret that were printed out from the script above and paste them in where appropriate. In this example we are using the client to get the user's information.

require 'fitgem'

consumer_key = 'your-app-consumer-key'
consumer_secret = 'your-app-consumer-secret'
token = 'token-received-in-above-script'
secret = 'secret-received-in-above-script'
user_id = 'your-user-id' # may be similar to '12345N'

client ={:consumer_key => consumer_key, :consumer_secret => consumer_secret, :token => token, :secret => secret, :user_id => user_id})
access_token = client.reconnect(token, secret)
p client.user_info

You can use this script to learn about the data structures that are returned from different API calls. Since this library always retrieves JSON responses and then parses it into Ruby structures, you can interrogate the response data with simple calls to hashes.

Note: Your user id is visible in the URL when viewing your profile, for example

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.