简体   繁体   English

尝试从Google OAuth 2.0获取access_token时为什么会出现404错误?

[英]Why do I get a 404 Error when attempting to get an access_token from google OAuth 2.0?

I'm currently building a OAuth authorization to get email and profile info only. 我目前正在建立OAuth授权,仅获取电子邮件和个人资料信息。

I can get the code easy using the following: 我可以使用以下代码轻松获得代码:

def authorize
  base = "https://accounts.google.com/o/oauth2/auth?"
  params = {
    :scope => "https://www.googleapis.com/auth/userinfo.profile",
    :redirect_uri => "http://localhost:3000/oauth/callback/",
    :client_id => "id",
    :response_type => 'code'
  }
  redirect_to base + params.to_query
end

However, when I try to get the access token from Google, I get a 404 error: 但是,当我尝试从Google获取访问令牌时,出现404错误:

def callback
  code = params[:code]
  client_id = 'id'
  client_secret = 'secret'
  redirect_uri = "http://localhost:3000/oauth/callback/"
  http = Net::HTTP.new('accounts.google.com', 80)
  path = "https://accounts.google.com/o/oauth2/token?"
  headers = {'Content-Type'=>"application/x-www-form-urlencoded" }
  parameters = "code=#{code}&grant_type=authorization_code&client_secret=#{client_secret}&client_id=#{client_id}&redirect_uri=#{redirect_uri}"
  resp, data = http.post(path,parameters,headers)
end

response code: 404  data: Google Home | Sign in

The page you requested is invalid.

I'm not entirely sure what the issue might be. 我不确定问题可能是什么。 I've tried to match with copy+paste the registered callback URL. 我试图匹配复制并粘贴已注册的回调URL。 I've checked my parameters multiple times and I cannot find what could be causing the error. 我已经多次检查了参数,但找不到导致错误的原因。

I'm following the Google documentation and registered with Google with the following information: 我正在遵循Google文档并已在Google注册以下信息:

Client ID for web applications 
Client ID: id
Client secret: secret
Redirect URIs: http://localhost:3000/oauth/callback/ 
JavaScript origins: none

Got it working! 得到它的工作!

Seems like i was missing a couple of things: 好像我错过了几件事:

  1. Added the SSL settings for the HTTP request. 为HTTP请求添加了SSL设置。
  2. Send the parameters as the body and not as part of the request. 将参数作为正文而不是作为请求的一部分发送。
  3. Put the content-type in the request correctly. 将内容类型正确放入请求中。

I hope this helps someone out there! 我希望这可以帮助某个人!

param = {
  :code => params[:code],
  :client_id => client_id,
  :client_secret => client_secret,
  :redirect_uri => "http://localhost:3000/oauth/callback/",
  :grant_type => 'authorization_code'
}

uri = URI.parse("https://accounts.google.com/o/oauth2/token")
http = Net::HTTP.new(uri.host, uri.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request["Content-Type"] = "application/x-www-form-urlencoded"
request.body = param.to_query
response = http.request(request)

If you are trying to get Google to redirect to: 如果您试图让Google重定向到:

:redirect_uri => "http://localhost:3000/oauth/callback/",

I suspect they can't find localhost . 我怀疑他们找不到localhost Give them your real IP number. 给他们您的真实IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spotify oauth 在尝试获取 access_token 时返回 400 - Spotify oauth is returning a 400 when trying to get access_token 如何从omniauth回调响应中获取access_token? - How do I get the access_token from the omniauth callback response? 获取ruby中的oauth2 access_token所有者电子邮件地址? - Get oauth2 access_token owner email address in ruby? 如果我已经具有access_token,如何获取omniauth哈希? - How to get omniauth hash if I already have the access_token? 来自具有永久access_token的Rails应用的GET请求 - GET request from rails app with permanent access_token 如何在删除用户帐户时重置omniauth-google-oauth2 access_token - How to reset omniauth-google-oauth2 access_token when user account has been removed 无法使用Rails和OAuth gem获得我的access_token - Can't get my access_token using Rails and the OAuth gem 尝试从Rails 3.1中的其他模型访问模型对象时,为什么会出现错误? - Why do I get an error when trying to access a model object from a different model in Rails 3.1? 无法获得Google AnalyticsAPI的oAuth2访问令牌 - Cannot get oAuth2 Access Token for Google Analytics API 尝试使用OAuth从Evernote获取请求令牌时出现401 oauth_signature错误 - Getting a 401 oauth_signature error when trying to get an request token from Evernote using OAuth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM