首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Withings创建通知始终响应342

Withings创建通知始终响应342
EN

Stack Overflow用户
提问于 2015-03-06 21:53:47
回答 1查看 296关注 0票数 0

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

我正在从控制器调用OauthUtil

代码语言:javascript
复制
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函数

代码语言:javascript
复制
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来组织参数和构建签名

代码语言:javascript
复制
 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签名

代码语言:javascript
复制
  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)是无效的引用函数,我认为是hook_url函数或签名函数的问题。感谢‘s

将参数更改为base_string更新为

代码语言:javascript
复制
@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
    }

更新基本字符串示例

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2015-03-10 19:52:57

尝试使用另一个参数,如withings文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28900274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档