我正在使用 gem OAuth2与 Google 服务进行通信。我不明白如何实现回调,该回调接收带有 OAuth 代码的响应以获取访问 token 。当我在回调方法中设置断点时,它似乎永远不会被调用。
这是我的代码:
路线:
match '/oauth2/callback' => 'reports#callback'
实际重定向的网址:
http://localhost/oauth2/callback?code=111111
报告 Controller :
def new
client = OAuth2::Client.new(ENV['GA_CLIENT_ID'], ENV['GA_SECRET_KEY'], {
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
:token_url => 'https://accounts.google.com/o/oauth2/token'
})
redirect_to client.auth_code.authorize_url({
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:redirect_uri => 'http://localhost/oauth2/callback',
:access_type => 'offline'
})
end
def callback
oauth_code = params[:code]
# Create access token with oauth_code
end
最佳答案
Google 服务器正在尝试访问此无效网址 http://localhost/oauth2/callback?code=111111
。
您需要域名才能使用 OAuth 等服务,因为 Google 服务器必须能够通过互联网找到您的计算机。
为了能够在您的开发机器上执行此操作,您应该:
关于ruby-on-rails - 如何配置oauth回调的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864199/