簡體   English   中英

redirect_uri_mismatch 與 Heroku 上的 OmniAuth Google oauth2

[英]redirect_uri_mismatch with OmniAuth Google oauth2 on Heroku

我正在嘗試使用谷歌身份驗證通過 OmniAuth 設置一個簡單的 Rails 應用程序。

在 heroku 上運行應用程序時,當我嘗試直接或通過重定向訪問 oauth 路由時出現以下錯誤:

redirect_uri_mismatch

索取詳情:

access_type=offline
client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com
redirect_uri=http://stock-scraper-rails.herokuapp.com/auth/google_oauth2/callback
response_type=code
scope=email profile
state=94be59d4d241b70c83406ce59c36e7fc8d50279c

在本地工作得很好 我嘗試使用 ngrok 隧道,它也有效。

完整網址: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 : https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 access_type https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 offline https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 client_id https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 redirect_uri https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8 3Astock%2F% https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=631910956855-pbglluk1ofb6vjmub9a0fucs8b0r5map.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fstock-scraper-rails.herokuapp.com%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=ac4cf27b4e2b534d854136ad25a102e2c1ff772d07dc84b8

我的應用程序托管在http://stock-scraper-rails.herokuapp.com您可以轉到 /auth/google_oauth2 自己查看錯誤。

我搜索了一下,但無法解決問題。 這是我已經嘗試/做過的,但沒有解決問題:

  • 將域添加到授權域
  • 類似問題的一些答案建議等待,因為有時谷歌需要一段時間才能更新域的更改。 但是,我已經等了幾個小時,錯誤仍然存​​在
  • 雙重/三重檢查我的環境變量在 Heroku 上是否正確
  • 檢查 Heroku 日志; 沒有錯誤
  • 手動設置 OmniAuth.config.full_host

回調路徑:

get '/auth/google_oauth2/callback', to: 'auth#oauth_callback'

順便說一下,我沒有使用設計。 目前我只希望控制器在會話中存儲一些數據:

class AuthController < ApplicationController

  def oauth_callback
    authentication_google_data = request.env['omniauth.auth'].except(:extra)
    user_email = authentication_google_data['info']['email']
    # rest ommited
  end
end

OmniAuth 配置:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end

相關寶石版本:

  • 導軌 (6.0.2.1)
  • omn​​iauth (1.9.0)
  • omn​​iauth-google-oauth2 (0.8.0)
  • omn​​iauth-oauth2 (1.6.0)

還嘗試將 omniauth-oauth 降級到 1.3.1,因為讀到有一個版本導致類似問題,但沒有成功。

關於我可以嘗試的任何其他想法都會非常有幫助:)

我想出了問題所在。 在我的應用程序的 google 開發者控制台上,在 OAuth 2.0 客戶端 ID 上,我創建了一個類型為“其他”而不是“Web 應用程序”的 ID。

https://console.cloud.google.com/apis/credentials?project=myproject上創建一個新的,類型為“Web 應用程序”並將回調 url(http 和 https)添加到授權重定向 URI解決了這個問題。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM