OAuth - “重定向 uri 不匹配”

问题描述 投票:0回答:0

我已经与这个 Google 登录斗争了一段时间(+2 周)并且拉扯我的头发,因为我不明白是什么导致了我这个错误。

上下文:Rails 7 API 试图发送一个 HTTP 请求来交换我的授权代码,以获得来自 Google 的访问令牌。
错误:

"error": "redirect_uri_mismatch",

GOOGLE_AUTHORIZATION_URL=https://oauth2.googleapis.com/token

# Redirect URI
REDIRECT_URI=https://63cd-106-185-157-124.ngrok.io/auth/google_oauth2/callback

Google Console

Ngrok Running

我获取授权码然后发送请求的方法:

 def self.get_access_token(auth_code)
    conn = Faraday.new
    response = conn.post(ENV['GOOGLE_AUTHORIZATION_URL']) do |req|
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      req.body = URI.encode_www_form(
        code: auth_code,
        client_id: ENV['GOOGLE_CLIENT_ID'],
        client_secret: ENV['GOOGLE_CLIENT_SECRET'],
        grant_type: 'authorization_code',
        redirect_uri: ENV['REDIRECT_URI']
      )
    end

    if response.status == 200
      puts "Successfully authorized code and got access_token"
      puts "Response body: #{response.body}"
    else
      puts "Failed to get access_token."
      puts "Response status: #{response.status}"
      puts "Response body: #{response.body}"
    end
  end

我开始用这个 redirect_uri 发送我的请求:

http://localhost:3000/auth/google_oauth2/callback
也添加到我的控制台中,但它给了我同样的错误
error: "redirect_uri_mismatch

当我在某处读到它是必需的时,我改用了 ngrok 和 HTTPS?

  • 多次重启我的应用程序
  • 添加我的 URI 后等待超过 24 小时
  • 尝试从头开始

感谢任何帮助。希望有人能指出我错过的一些愚蠢的事情。

ruby-on-rails api oauth google-signin redirect-uri-mismatch
© www.soinside.com 2019 - 2024. All rights reserved.