Question

I have been trying for days to pull down activity data from the Withings API using the OAuth Ruby gem. Regardless of what method I try I consistently get back a 503 error response (not enough params) even though I copied the example URI from the documentation, having of course swapped out the userid. Has anybody had any luck with this in the past. I hope it is just something stupid I am doing.

class Withings
  API_KEY = 'REMOVED'
  API_SECRET = 'REMOVED'
  CONFIGURATION = { site: 'https://oauth.withings.com', request_token_path: '/account/request_token',
                        access_token_path: '/account/access_token', authorize_path: '/account/authorize' }

  before do
    @consumer = OAuth::Consumer.new API_KEY, API_SECRET, CONFIGURATION
    @base_url ||= "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['SCRIPT_NAME']}"
  end

  get '/' do
    @request_token = @consumer.get_request_token oauth_callback: "#{@base_url}/access_token"
    session[:token] = @request_token.token
    session[:secret] = @request_token.secret

    redirect @request_token.authorize_url
  end

  get '/access_token' do
    @request_token = OAuth::RequestToken.new @consumer, session[:token], session[:secret]

    @access_token = @request_token.get_access_token oauth_verifier: params[:oauth_verifier]
    session[:token] = @access_token.token
    session[:secret] = @access_token.secret
    session[:userid] = params[:userid]

    redirect "#{@base_url}/activity"
  end

  get '/activity' do
    @access_token = OAuth::AccessToken.new @consumer, session[:token], session[:secret]
    response = @access_token.get("http://wbsapi.withings.net/v2/measure?action=getactivity&userid=#{session[:userid]}&startdateymd=2014-01-01&enddateymd=2014-05-09")
    JSON.parse(response.body)
  end
end

For other API endpoints I get an error response of 247 - The userid provided is absent, or incorrect. This is really frustrating. Thanks

Was it helpful?

Solution

So I figured out the answer after copious amount of Googleing and grasping a better understanding of both the Withings API and the OAuth library I was using. Basically Withings uses query strings to pass in API parameters. I though I was going about passing these parameters correctly when I was making API calls, but apparently I needed to explicitly set the OAuth library to use the query string scheme, like so

http_method: :get, scheme: :query_string

This is appended to my OAuth consumer configuration and all worked fine immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top