Get Fitibt Access token bu oauth2 authentication.
Get the code by setting up omniauth callback URL:-
redirect_to "https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=#{ENV['FITBIT_CLIENT_ID']}&redirect_uri=#{$URL}%2Fauth%2Ffitbit_oauth2%2Fcallback%2F&scope=activity%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight&expires_in=604800"
You will get code in params[:code], then use this code in following script;-
uri = URI.parse("https://api.fitbit.com/oauth2/token")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
header_string = Base64.encode64(("#{ENV['FITBIT_CLIENT_ID']}:#{ ENV['FITBIT_CONSUMER_SECRET']}"))
request.set_form_data('clientId' => ENV['FITBIT_CLIENT_ID'], 'grant_type' => 'authorization_code', 'redirect_uri' => $URL + '/auth/fitbit_oauth2/callback/', 'code' => params[:code])
request.initialize_http_header({"Authorization" => "Basic " + header_string, "Content-Type" => "application/x-www-form-urlencoded"})
response = http.request(request)
unless response.is_a?(Net::HTTPSuccess)
flash[:notice] = 'We currently do not offer account linking with that'
return redirect_to "/"
end
json = ActiveSupport::JSON.decode(response.body)
redirect_to "https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=#{ENV['FITBIT_CLIENT_ID']}&redirect_uri=#{$URL}%2Fauth%2Ffitbit_oauth2%2Fcallback%2F&scope=activity%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight&expires_in=604800"
You will get code in params[:code], then use this code in following script;-
uri = URI.parse("https://api.fitbit.com/oauth2/token")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
header_string = Base64.encode64(("#{ENV['FITBIT_CLIENT_ID']}:#{ ENV['FITBIT_CONSUMER_SECRET']}"))
request.set_form_data('clientId' => ENV['FITBIT_CLIENT_ID'], 'grant_type' => 'authorization_code', 'redirect_uri' => $URL + '/auth/fitbit_oauth2/callback/', 'code' => params[:code])
request.initialize_http_header({"Authorization" => "Basic " + header_string, "Content-Type" => "application/x-www-form-urlencoded"})
response = http.request(request)
unless response.is_a?(Net::HTTPSuccess)
flash[:notice] = 'We currently do not offer account linking with that'
return redirect_to "/"
end
json = ActiveSupport::JSON.decode(response.body)
Comments
Post a Comment