Instagram上的Omniauth on Rails invalid_credentials

时间:2016-12-15 23:35:32

标签: ruby-on-rails ruby oauth instagram omniauth

我已经有一个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

基本上它会创建一个新用户,如果他们没有连接就会签名,如果他们有,则会更新他们的登录信息。

1 个答案:

答案 0 :(得分:3)

在Instagram上似乎有一个关于获取访问令牌的问题。 https://news.ycombinator.com/item?id=13178789