Last active
January 30, 2019 10:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
oauth: | |
consumer_key: "your consumer key here" | |
consumer_secret: "your consumer secret here" | |
ssl: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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