【发布时间】:2016-12-15 23:35:31
【问题描述】:
我的 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
我的路线只是您的基本全域认证路线:
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
基本上,它会创建一个新用户并在他们未连接时登录,如果已连接,它会更新他们的登录信息。
【问题讨论】:
-
您能否添加更多详细信息,例如您的路由文件和接收回调的控制器?您使用的是哪个版本的 Omniauth 和 Instagram Omniauth?
-
@AlexanderLuna 我已在包含所有这些信息的帖子中添加了一个编辑。让我知道这是否有帮助。
-
我有个更好的主意,谢谢,但是您的“from_omniath”方法看起来如何?
-
遇到同样的问题。
标签: ruby-on-rails ruby oauth instagram omniauth