我想使用MapMyFitness API(www.mapmyapi.com)與Ruby on Rails 3.2和oauth2寶石。首先,我的應用程序在「get_auth_url」中生成auth_url。瀏覽器然後導航到它,一旦認證,回調就會返回到「mapmyfitness_callback」。 「mapmyfitness_callback」也獲得「鍛鍊」列表,並在瀏覽器中顯示。Oauth2錯誤與MapMyFitness API
問題出在用戶選擇要下載的鍛鍊時。要檢索選定的鍛鍊,我稱之爲「get_workout」。但是,我很難獲取請求的相應標記。
下面崩潰的行:
workout_data = access_token.get('/v7.0/workout/' + workout_id, :params => { 'field_set' => 'time_series' }, :headers => {'Api-Key' => ENV['MMF_API_KEY'], 'Authorization' => auth_token}).body
與:OAuth2用戶::錯誤(: { 「oauth1_error」: 「格式錯誤的授權頭」, 「oauth1_error_code」: 「您好!OAuth1:UNKNOWN」}): 應用/controllers/telemetry_controller.rb:60:in`get_workout」
整個控制器的代碼:
require 'oauth2'
class TelemetryController < ApplicationController
def get_auth_url
auth_url = mmf_client.auth_code.authorize_url(:redirect_uri => 'http://localhost:3000/telemetry/mapmyfitness_callback')
respond_to do |format|
format.json{ render :json => {:auth_url => auth_url}.to_json }
end
end
def mapmyfitness_callback
# Get user
@code = params[:code]
token = mmf_client.auth_code.get_token(@code, :redirect_uri => 'http://localhost:3000/telemetry/mapmyfitness_callback', :headers => {'Api-Key' => ENV['MMF_API_KEY']})
mmf_user = JSON.parse(token.get('/v7.0/user/self', :headers => {'Api-Key' => ENV['MMF_API_KEY'], 'Authorization' => @code}).body)
mmf_user_id = mmf_user['id']
@auth_token = token.token
# Get workouts
mmf_workouts = JSON.parse(token.get('/v7.0/workout', :params => { 'user' => mmf_user_id }, :headers => {'Api-Key' => ENV['MMF_API_KEY'], 'Authorization' => @code}).body)
@workout_list = Array.new
mmf_workouts['_embedded']['workouts'].each do |workout|
workout_data = {:name => workout['name'],
:id => workout['_links']['self'][0]['id']}
@workout_list.push(workout_data)
end
render :layout => false
end
def get_workout
code = params[:code]
auth_token = params[:auth_token]
access_token = OAuth2::AccessToken.new(mmf_client, auth_token, {
:mode => :query,
:param_name => "oauth2_access_token",
})
puts access_token.to_yaml
# Get workout
workout_id = params[:workout_id]
workout_data = access_token.get('/v7.0/workout/' + workout_id, :params => { 'field_set' => 'time_series' }, :headers => {'Api-Key' => ENV['MMF_API_KEY'], 'Authorization' => auth_token}).body
respond_to do |format|
format.json{ render :json => {:mmf_workout_data => workout_data}.to_json }
end
end
private
def mmf_client
client = OAuth2::Client.new(
ENV['MMF_API_KEY'],
ENV['MMF_SECRET_KEY'],
:authorize_url => "https://www.mapmyfitness.com/v7.0/oauth2/authorize/",
:token_url => "https://oauth2-api.mapmyapi.com/v7.0/oauth2/access_token/",
:site => "https://oauth2-api.mapmyapi.com"
)
end
end
你能否給出更多解釋爲什麼以前的代碼不起作用? –