Reputation: 31
I'm trying to setup up an oauth2 webapp (front end written in react, backend in rails). On the front end I'm able to get authenticate and ket my access code, after that through my callback function on redirected to my back server, where I'm trying to exchange my front-end code for a token, below is my code on the back end.
I am initializing a new auth_client and it is being updated properly (client_secrets and code). The problem is when I'm requesting my exchange token, it is giving me a
*** Signet::AuthorizationError Exception: Authorization failed. Server message:
{
"error" : "redirect_uri_mismatch"
}
I don't know how to solve that, the redirect address is being loaded from the client_secret, I've confirmed it, I tried to update it again using auth_ ient.update!, same problems. My routes do exist, tried them, I was using localhost before, but was getting same error, through web search was able to find recommendations of the use of lvh.me. I also tried to send it to a different controller (route) http://localhost:3000/api/v1/users which also have Post, but same error....
I don't know what else to try, I would really appreciate if someone could give me some direction, this is for a capstone project due on Wednesday, and I everything else depends on it...
Thank you in advance for any help ....
require 'google/api_client/client_secrets'
class Api::V1::AuthController < ApplicationController
def create
client_secrets= Google::APIClient::ClientSecrets.load("client_secrets.json")
auth_client = client_secrets.to_authorization
auth_client.update!(
:scope => 'profile https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.send',
:redirect_uri => 'http://lvh.me:3000/api/v1/auth'
)
auth_client.code = params["code"]
result = auth_client.fetch_access_token! <-----Breaks here------->
end
end
My routes...
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users
post '/auth',to:'auth#create'
get '/current_user', to: 'auth#show'
end
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
My Authorized redirect URIs on Google API Dashboard ...
http://lvh.me:3000/api/v1/auth
http://localhost:3000/api/v1/auth
http://localhost:3000/api/v1/users
http://lvh.me:3000/api/v1/users
My client_secrets.json ...
client_secrets.json
{
"web": {
"client_id":
"<MY_CLIENT_ID",
"project_id": "storied-pixel-191717",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "<MY_CLIENT_SECRET>",
"redirect_uris": ["http://lvh.me:3000/api/v1/auth", "http://localhost:3000/api/v1/auth","http://localhost:3000/api/v1/users","http://lvh.me:3000/api/v1/users"],
"scope":
"profile https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.send",
}
}
Upvotes: 1
Views: 331
Reputation: 31
Ok... I was able to get it to work, only had to replace my redirect_uri with a 'postmessage'. That did the trick for me.
Upvotes: 2