Withings API:回调网址未知或无效

时间:2015-09-21 22:02:44

标签: ruby-on-rails ruby withings

我试图在withings API中设置通知(使用withings-simplificator gem)。无论我输入什么URL或是否编码,我总是会收到此错误:

irb(main):013:0> user.subscribe_notification('http://foo.bar.com', 'test subscription')
Withings::ApiError: The callback URL 'http://foo.bar.com' is either unknown or invalid - Status code: 293
  from /app/vendor/bundle/ruby/2.2.0/gems/simplificator-withings-0.7.0/lib/withings/connection.rb:80:in `verify_response!'
  from /app/vendor/bundle/ruby/2.2.0/gems/simplificator-withings-0.7.0/lib/withings/connection.rb:22:in `get_request'
  from /app/vendor/bundle/ruby/2.2.0/gems/simplificator-withings-0.7.0/lib/withings/connection.rb:27:in `get_request'
  from /app/vendor/bundle/ruby/2.2.0/gems/simplificator-withings-0.7.0/lib/withings/user.rb:26:in `subscribe_notification'
  from (irb):13
  from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
  from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
  from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
  from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
  from bin/rails:8:in `require'
  from bin/rails:8:in `<main>'

是否有人遇到此问题并有解决方案?

更新1:

所以我尝试了没有withings的simpleificator gem:

API_KEY       = '123'
API_SECRET    = '456'
USER_ID       = '789'
USER_KEY      = 'abc'
USER_SECRET   = 'def'
CONFIGURATION = {               site: 'https://oauth.withings.com',
                  request_token_path: '/account/request_token',
                   access_token_path: '/account/access_token',
                      authorize_path: '/account/authorize',
                         http_method: :get,
                              scheme: :query_string
                }

@consumer = OAuth::Consumer.new API_KEY, API_SECRET, CONFIGURATION

@access_token = OAuth::AccessToken.new @consumer, USER_KEY, USER_SECRET

url = ERB::Util.url_encode("www.foo.bar/trigger")
comment = ERB::Util.url_encode("Trigger")
response = @access_token.get("https://wbsapi.withings.net/notify?action=subscribe&userid=#{USER_ID}&callbackurl=#{url}&comment=#{comment}")
JSON.parse(response.body)

同样的错误:

irb(main):051:0>   JSON.parse(response.body)
=> {"status"=>293}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您必须在安装过程中确保

  • 你的&#34;触发网址&#34;存在
  • 它回应&#34; ok&#34;到一个POST
  • 它很快......我无法确切地知道这有多快,但我认为响应时间应该是&lt; 1秒

在这样的环境下,我的代码可以正常工作。

答案 1 :(得分:0)

您的通知端点将执行两项操作:

  1. 它将响应测试请求响应(在注册时将检查您的URL是否存在)。您将收到带有空身体对象的呼叫。如果没有userid,则它不是数据通知,因此您必须将状态为200的状态为空。
  2. 成功注册后,您将收到数据警报。这次您可以按照业务逻辑处理。