Search code examples
ruby-on-railsapiopenstreetmapomniauth

How to get OpenStreetMap access token with devise omniauth-osm


In my controller, I'm trying to get an access token to OSM.

class Auth::OauthController < Devise::OmniauthCallbacksController

def osm
  @user = AppUser.from_omniauth(request.env["omniauth.auth"])

  token_req = request.env["omniauth.auth"]['extra']['access_token'].consumer.get_request_token(:oauth_verifier => params['oauth_verifier'])   

  @user.token = token_req.token
  @user.token_secret = token_req.secret

  sign_in_and_redirect @user
end

end

When I get the access token and writes it to the database.

Next, I try to use the OSM API through oauth gem.

@consumer=OAuth::Consumer.new Settings.osm.consumer_key, 
                          Settings.osm.consumer_secret, 
                          {site: osm_uri}

@access_token = OAuth::AccessToken.new(@consumer, current_user.token, current_user.token_secret) 

puts @access_token.get('/api/0.6/user/preferences').body

However, this code does not work in the console I see the authorization error


Solution

  • the error in this code:

    token_req = request.env["omniauth.auth"]['extra']['access_token'].consumer.get_request_token(:oauth_verifier => params['oauth_verifier'])   
    
    @user.token = token_req.token
    @user.token_secret = token_req.secret
    

    correct code

    @app_user.token = request.env["omniauth.auth"]['credentials']['token']
    @app_user.token_secret = request.env["omniauth.auth"]['credentials']['secret']