在我的控制器中,我试图获取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
当我获得访问令牌并将其写入数据库时。
接下来,我尝试通过oauth gem使用OSM API。
@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
但是,此代码在控制台中不起作用我看到了授权错误
答案 0 :(得分:0)
此代码中的错误:
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
正确的代码
@app_user.token = request.env["omniauth.auth"]['credentials']['token']
@app_user.token_secret = request.env["omniauth.auth"]['credentials']['secret']