我正在尝试建立一个oauth2 webapp(前端写在反应中,后端在rails中)。在前端,我能够获得身份验证和访问代码,之后通过我的回调函数重定向到我的后台服务器,我正在尝试将我的前端代码替换为令牌,下面是我的代码在后端。
我正在初始化一个新的auth_client并且它正在正确更新(client_secrets和code)。问题是当我要求我的兑换令牌时,它正在给我一个
*** Signet :: AuthorizationError异常:授权失败。服务器消息:
{
“error”:“redirect_uri_mismatch”
}
我不知道如何解决这个问题,重定向地址是从client_secret加载的,我已经确认了,我尝试使用auth_ient.update再次更新它!同样的问题。我的路线确实存在,尝试过它们,之前我使用的是localhost,但是得到了相同的错误,通过网络搜索能够找到使用lvh.me的建议。我也尝试将它发送到另一个控制器(路由)http://localhost:3000/api/v1/users,它也有Post,但同样的错误......
我不知道还有什么可以尝试,我真的很感激,如果有人可以给我一些方向,这是针对周三到期的顶点项目,我其他一切都取决于它......
提前感谢您的帮助......
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
我的路线......
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
Google API信息中心上的我的授权重定向URI ...
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
我的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",
}
}
答案 0 :(得分:1)
好的......我能够让它工作,只需要用'postmessage'替换我的redirect_uri。这对我有用。