Rails invalid_credentials 上的 Instagram Omniauth

Instagram Omniauth on Rails invalid_credentials

我的 Instagram omniauth 集成已经运行了几年,很长一段时间我都没有改变任何东西,但我的客户在连接他们的 Instagram 帐户时偶尔会突然出现错误:

Started GET "/auth/instagram/callback?code=d3b1c4d88e2f440b8a8a98037b821c15&state=2565d32ecd3cc5967f32d8c945db6ffba74dc784100777f2" for 127.0.0.1 at 2016-12-15 15:29:59 -
0800
I, [2016-12-15T15:29:59.276712 #32520]  INFO -- omniauth: (instagram) Callback phase initiated.
E, [2016-12-15T15:29:59.519801 #32520] ERROR -- omniauth: (instagram) Authentication failure! invalid_credentials: OAuth2::Error, :
{"code": 400, "error_type": "OAuthException", "error_message": "Matching code was not found or was already used."}

我能一直做到这一点的唯一方法是从我的应用程序连接帐户,然后从 instagram 端删除权限,然后尝试在我的应用程序上再次重新连接,但我的一些客户似乎是他们第一次连接他们的 Instagram 帐户时得到这个。

有人遇到过这个吗?我的 config/initializers/omniauth.rb 看起来像这样:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :instagram, ENV['INSTAGRAM_CLIENT_ID'], ENV['INSTAGRAM_CLIENT_SECRET'], scope: 'basic comments public_content'
end

编辑:

我正在使用 omniauth 1.3.1 和 omniauth-instagram 1.0.2

我的路线就是您的基本 omniauth 路线:

 get '/auth/:provider/callback', to: 'omniauth_callbacks#create'

我的控制器操作有点复杂,但看起来像这样:

class OmniauthCallbacksController < ApplicationController

  def create
    if user_signed_in?
      social_account = SocialAccount.from_omniauth(auth_params, current_user)
      social_account.save!
      redirect_to root_path
    else
      user = User.from_omniauth auth_params
      sign_in_and_redirect user
    end
  end

  private

  def auth
    request.env['omniauth.auth']
  end

  def auth_params
    {
      provider: auth['provider'],
      uid: auth['uid'],
      token: auth['credentials']['token'],
      secret: auth['credentials']['secret'] || auth['credentials']['refresh_token'],
      name: auth['info']['nickname'] || auth['info']['name'],
      emails_sent: 0
    }
  end
end

基本上它会创建一个新用户并在他们尚未连接时让他们登录,如果他们已经连接它会更新他们的登录信息。

在 Instagram 上获取访问令牌似乎存在问题。 https://news.ycombinator.com/item?id=13178789