【发布时间】:2018-01-13 03:57:03
【问题描述】:
我正在尝试设置一个 oauth2 webapp(前端用 react 编写,后端用 rails 编写)。在前端,我能够获得身份验证并获取我的访问代码,然后通过我的回调函数重定向到我的后端服务器,在那里我试图将我的前端代码交换为令牌,下面是我的代码在后端。
我正在初始化一个新的 auth_client 并且它正在正确更新(client_secrets 和代码)。问题是当我请求我的交换令牌时,它给了我一个
*** Signet::AuthorizationError 异常:授权失败。服务器消息:
{
“错误”:“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",
}
}
【问题讨论】:
-
我认为它期望回调请求是
get请求而不是post检查并将其更改为post '/auth',to:'auth#create'=>get '/auth',to:'auth#create' -
感谢您的回复,我认为它必须是一个发布路线,因为他们会发回数据,无论如何......我让它工作了,只需用'postmessage'替换我的redirect_uri .非常感谢......
标签: ruby-on-rails google-api google-oauth google-api-ruby-client