我的 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'
我的 Controller 操作有点复杂,但看起来像这样:
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 上似乎在获取访问 token 方面存在问题。 https://news.ycombinator.com/item?id=13178789
关于ruby-on-rails - Instagram Omniauth on Rails invalid_credentials,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41175074/