使用Jawbone Up API和Ruby on Rails进行401未授权错误

时间:2014-08-19 07:29:36

标签: ruby-on-rails ruby oauth omniauth httparty

我试图通过Omniauth,HTTParty& amp;来访问Jawbone API。 Ruby on Rails。我正在通过标题Authorization =>持票人:[doc_here]根据API文档的要求,但它仍然给我401错误,我不能为我的生活找出原因。我能够通过omniauth成功获取令牌并将其存储到会话[:token],但API会在未经授权的情况下返回401.

API文档:https://jawbone.com/up/developer/authentication

我搜索了堆栈溢出,无法找到解决方案。有什么问题?

我的代码是:

sessions_controller.rb

def create
auth_hash = request.env['omniauth.auth']

if session[:user_id]
    #if there's a session, then create the authorization
    User.find(session[:user_id]).add_provider(auth_hash)

else 
    auth = Authorization.find_or_create(auth_hash)
    session[:user_id] = auth.user.id
    session[:token] = auth.user.authorizations.first.token


end
redirect_to '/today'
end

data_processor.rb

class DataProcessorController < ApplicationController
  def today
    @current_user_dp = session[:user_id]
    @token_dp = session[:token]
    @authorization_dp = "Bearer " + @token_dp
    #@auth = "Bearer " + session[:current_user].authorizations.first.token unless session[:user_id].nil?
    @result = HTTParty.get('http://jawbone.com/nudge/api/v.1.1/users/@me/goals',
                           :headers => { "Authorization" => @authorization_dp, "Accept" => "application/json"}) unless session[:token].nil?

  end
end

results.html.erb

<p>Result: <%= @result %></p>
<p>Auth header: <%= @result.request.inspect%></p>

HTML文件的输出:

Result: {"meta"=>{"code"=>401, "error_detail"=>"You must be logged in to perform that action", "error_type"=>"authentication_error", "message"=>"Unauthorized"}, "data"=>{}}

Auth header: #<HTTParty::Request:0x007fe446bb3320 @http_method=Net::HTTP::Get, @path=#<URI::HTTPS:0x007fe446bbb408 URL:https://jawbone.com/nudge/api/v.1.1/users/@me/goals>, @options={:limit=>4, :default_params=>{}, :follow_redirects=>true, :parser=>HTTParty::Parser, :connection_adapter=>HTTParty::ConnectionAdapter, :headers=>{"Authorization"=>"Bearer XXXXXXXXXTOKEN_HEREXXXXXXXXXXXXX", "Accept"=>"application/json"}}, @last_uri=#<URI::HTTPS:0x007fe446bba918 URL:https://jawbone.com/nudge/api/v.1.1/users/@me/goals>, @raw_request=#<Net::HTTP::Get GET>, @last_response=#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>, @redirect=true>

2 个答案:

答案 0 :(得分:1)

你解决了这个问题吗?

如果您查看其他错误字段,它可能会为您提供更多信息。在我的情况下,我得到了未经授权的范围&#39;。我在原始oauth请求中未包含的范围内请求项目,因此我提供的令牌范围有限。

答案 1 :(得分:0)

首次授权用户时,请尝试更改范围。我使用scope=move_read并且能够从http://jawbone.com/nudge/api/v.1.1/users/@me/goals获取数据。