Skip to content

Instantly share code, notes, and snippets.

@whazzmaster
Last active January 30, 2019 10:54
Show Gist options
  • Save whazzmaster/5322902 to your computer and use it in GitHub Desktop.
Save whazzmaster/5322902 to your computer and use it in GitHub Desktop.
Bench-test script for playing around with the Fitbit API and fitgem in using Ruby. For more information see http://fitbitclient.com
oauth:
consumer_key: "your consumer key here"
consumer_secret: "your consumer secret here"
ssl: true
#!/usr/bin/env ruby
require "fitgem"
require "pp"
require "yaml"
# Load the existing yml config
config = begin
Fitgem::Client.symbolize_keys(YAML.load(File.open(".fitgem.yml")))
rescue ArgumentError => e
puts "Could not parse YAML: #{e.message}"
exit
end
client = Fitgem::Client.new(config[:oauth])
# With the token and secret, we will try to use them
# to reconstitute a usable Fitgem::Client
if config[:oauth][:token] && config[:oauth][:secret]
begin
access_token = client.reconnect(config[:oauth][:token], config[:oauth][:secret])
rescue Exception => e
puts "Error: Could not reconnect Fitgem::Client due to invalid keys in .fitgem.yml"
exit
end
# Without the secret and token, initialize the Fitgem::Client
# and send the user to login and get a verifier token
else
request_token = client.request_token
token = request_token.token
secret = request_token.secret
puts "Go to https://www.fitbit.com/oauth/authorize?oauth_token=#{token} and then enter the verifier code below"
verifier = gets.chomp
begin
access_token = client.authorize(token, secret, { :oauth_verifier => verifier })
rescue Exception => e
puts "Error: Could not authorize Fitgem::Client with supplied oauth verifier"
exit
end
puts 'Verifier is: '+verifier
puts "Token is: "+access_token.token
puts "Secret is: "+access_token.secret
user_id = client.user_info['user']['encodedId']
puts "Current User is: "+user_id
config[:oauth].merge!(:token => access_token.token, :secret => access_token.secret, :user_id => user_id)
# Write the whole oauth token set back to the config file
File.open(".fitgem.yml", "w") {|f| f.write(config.to_yaml) }
end
# ============================================================
# Add Fitgem API calls on the client object below this line
pp client.activities_on_date 'today'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment