I've been battling with this Google Login for a while now(+2 weeks) and pulling my hair because I don't understand what is causing this error for me.
Context: Rails 7 API trying to send a HTTP request to eXchange my Authorization Code for an access token from Google.
Error: "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]
My method getting the authorization code and then sending the request:
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
I started out with sending my request with this redirect_uri: http://localhost:3000/auth/google_oauth2/callback
whichis also added in my console, but it gave me the same error error: "redirect_uri_mismatch
I changed over to using ngrok and HTTPS as I read somewhere that it is required?
- Restarted my app plenty of times
- Waited for over 24 hours after adding my URI
- Tried restarting from scratch
Grateful for any help. Hope someone can point out something stupid I missed.