I have to try Subscribe notification to withings use this doc 👋 Welcome | Withings i am using rails
I am call OauthUtil from controller
oauth = OauthUtil.new oauth.consumer_key = WITHINGS_API_KEY oauth.consumer_secret = WITHINGS_API_SECRET oauth.token = profile.oauth_token # Token from step 3 oauth.token_secret = profile.token_secret # Token secret from step 3 oauth.userid = profile.uid In OauthUtil.rb i am create function to call url
def register_webhook parsed_url = URI.parse( WITHINGS_URL + '/notify' )
Net::HTTP.start( parsed_url.host ) { | http | req = Net::HTTP::Get.new "#{ parsed_url.path }?#{ self.hook_url(parsed_url).query_string }" response = http.request(req) response.read_body } end Add function hook_url to organize param and build signature
def hook_url( parsed_url )
@params = { 'action' => 'subscribe', 'callbackurl' => @callback_url, 'comment' => @comment, 'oauth_consumer_key' => @consumer_key, 'oauth_nonce' => nonce, 'oauth_signature_method' => @sig_method, 'oauth_timestamp' => Time.now.to_i.to_s, 'oauth_token' => @token, 'oauth_version' => @oauth_version, 'userid' => @userid }
# if url has query, merge key/values into params obj overwriting defaults if parsed_url.query @params.merge! CGI.parse( parsed_url.query ) end
# @ref OAuth Core 1.0 @req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path
# create base str. make it an object attr for ez debugging # ref OAuth Core 1.0 @base_str = [ @req_method, percent_encode( req_url ),
# normalization is just x-www-form-urlencoded percent_encode( query_string )
].join( '&' )
# add signature @params[ 'oauth_signature' ] = signature return self end Then function signature to create oauth signature
def signature key = percent_encode( @consumer_secret ) + '&' + percent_encode( @token_secret )
# ref: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks digest = OpenSSL::Digest.new( 'sha1' ) hmac = OpenSSL::HMAC.digest( digest, key, @base_str )
# ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81 Base64.encode64( hmac ).chomp.gsub( /\n/, '' ) end But always response 342 : The signature (using Oauth) is invalid reference A utility for signing an url using OAuth in a way that's convenient for debugging · GitHub i think the problem on hook_url function or signature function. Thank's
Update i'm change params to base_string to
@params = { 'action' => 'subscribe', 'oauth_consumer_key' => @consumer_key, 'oauth_nonce' => nonce, 'oauth_timestamp' => Time.now.to_i.to_s, 'oauth_token' => @token, 'oauth_signature_method' => @sig_method, 'oauth_version' => @oauth_version, 'userid' => @userid } Update base string example
GET&https%3A%2F%2Fwbsapi.withings.net%2Fnotify&action%3Dsubscribe%26callbackurl%3Dhttp%253A%252F%252Fstaging.medic-trust.com%252Fwearables%252Fwebhooks%252Fwithings%26comment%3DMedicTrust%26oauth_consumer_key%3D40b7c20955f4193d4ce51248f5c0921281f99483fdd5c6857b0d2974cd2340%26oauth_nonce%3Dc2e7423a12%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1426142430%26oauth_token%3Df9c32509ef7b690e88b2f17e1ddcfb0ed957e420ee02be2328f6f252329d%26oauth_version%3D1.0%26userid%3D6689931 But still response 342