Withings创建通知始终响应342

时间:2015-03-06 13:53:47

标签: ruby-on-rails oauth withings

我必须尝试使用​​订阅通知使用此文档http://oauth.withings.com/api/doc#api-Notification-notify_subscribe我正在使用rails

我从控制器

调用OauthUtil
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

在OauthUtil.rb中,我创建了调用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

添加函数hook_url以组织param并构建签名

 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 http://oauth.net/core/1.0/#rfc.section.9.1.2
    @req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path

    # create base str. make it an object attr for ez debugging
    # ref http://oauth.net/core/1.0/#anchor14
    @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

然后使用函数签名创建oauth签名

  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

但总是响应342:签名(使用Oauth)是无效的引用https://gist.github.com/erikeldridge/383159我认为hook_url函数或签名函数存在问题。感谢' S

更新我将params更改为base_string为

@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
    }

更新基本字符串示例

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

但仍然回应342

1 个答案:

答案 0 :(得分:0)

尝试使用其他参数,例如withings documentation