3

嗨,我有一个带有 ember-cli-simple-auth-torii 前端的 ember 应用程序和带有devise和 omniauth-facebook 后端的ember-cli-simple-auth- devise 后端

使用 facebook 登录时,鸟居会为您提供一个授权码,我们如何使用此授权码取决于我们。

因为根据服务器端对用户进行身份验证是一种很好的做法。我想将此授权代码与全方位身份验证一起使用。

我的 AuthenticationController 看起来像这样

class AuthenticationsController < Devise::OmniauthCallbacksController
 def facebook
   omniauth = request.env["omniauth.auth"]
   authentication = Authentication.find_by_provider_and_uid(omniauth['provider'],  omniauth['uid'])
    ...
    sign_in(:user,user)
 end
end

我的会话控制器

class SessionsController < Devise::SessionsController
  def create
    respond_to do |format|
      format.html { super }
      format.json do
        binding.pry
        self.resource = warden.authenticate!(auth_options)
        sign_in(resource_name, resource)
        data = {
          user_token: self.resource.authentication_token,
          user_email: self.resource.email
        }
        render json: data, status: 201
      end
    end
  end
end

我不确定我的方法是否正确,但我想users/auth/facebook/callback 从我的客户端调用应该触发服务器端的身份验证过程,我可以稍后授权用户在我的应用程序中进行 crud 操作。

authenticateWithFacebook: function(provider) {
       var self = this
        this.get('session').authenticate('simple-auth-authenticator:torii', "facebook-oauth2" ).then(function() {
          var authorizationCode= self.get('session.authorizationCode');
          console.log(authorizationCode);
          Ember.$.ajax({
            type: 'POST',
            url: 'http://localhost:3000/users/auth/facebook/callback',
            dataType: 'json',
            data: {
              code: authorizationCode,
            },
            success: function(data, textStatus, jqXHR) {
              // Handle success case
            },
            error: function(jqXHR, textStatus, errorThrown) {
              // Handle error case
            }
          });

        });
    },

我的服务器日志说我能够启动omniauth facebook登录回调阶段,但随后它给出了错误Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request

Started POST "/users/auth/facebook/callback" for 127.0.0.1 at 2014-11-16 11:03:44 +0530
I, [2014-11-16T11:03:44.926842 #5160]  INFO -- omniauth: (facebook) Callback phase initiated.
E, [2014-11-16T11:03:46.185161 #5160] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials: OAuth2::Error, : 
{"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100}}
Processing by AuthenticationsController#failure as HTML
  Parameters: {"code"=>"AQBaag8FhEzyd8qCMh14HbAl-iBXrpK1YSrP9vz72kzRE86S-cf0Vsf1sSfpR1-Fajr1QfUbAoyYqj3ivcXayGk5KcmT27b4avy1NAcLzM2FcW1neGS9RA6CoVhYXpj2rbjYY7Dm-1Qw6Me0RjiidwJxwF4SVUVX4S6Y5UatRMW6FW2IyKxJJy8e0-VYlmFBpv3VKjq3tYE_pdM6lKLTEBAyApvIm2UfTZXLqeWWIIIf3romLB-q48BXvv2koM5fSkrvB2HyPOJq9Y_RLeWtw4nARn8aluJC-KhyYfUcprf_KzM30ZBYNxu5S6IYkgcdq_kwEsHinoddDqe-"}
Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 62ms (ActiveRecord: 0.0ms)
  1. 验证验证码时出错。请确保您的 redirect_uri 与您在 OAuth 对话请求中使用的相同 我从客户端端口调用 Facebook 服务器:4200 并且我的 ajax 调用使用 url http://localhost:3000/users/auth/facebook/callback端口 3000

  2. 当我从客户端调用 ajax 时,我使用 /users/auth/facebook/callback 我收到错误:无效重定向 ie type: 'POST', url: '/users/auth/facebook/callback', dataType: 'json',

    在 2014-11-16 11:27:40 +0530 I,[2014-11-16T11:27:40.150441 #5160] INFO --omniauth:( facebook) 回调阶段启动。E,[2014-11-16T11:27:41.336997 #5160] 错误 --omniauth: (facebook) 身份验证失败!invalid_credentials: OAuth2::Error, : {"error":{"message":"Invalid redirect_uri: \u0926\u093f\u0932\u0947\u0932\u0940 URL \u0905\u0928\u0941\u092a\u094d\u0930\u092f\ u094b\u0917 \u0915\u0949\u0928\u094d\u092b\u093f\u0917\u0930\u0947\u0936\u0928\u0926\u094d\u0935\u093e\u0930\u0947 \u0905\u0928\u0941\u092e\u0924 \u0928\ u093e\u0939\u0940.","type":"OAuthException","code":191}} AuthenticationsController#failure 作为 JSON 参数处理:{"code"=>"

我不知道我的方法是否正确。我想同时拥有设计 + 牌坊认证

4

1 回答 1

0

//这对我有用,等待更优雅的方式。

   authenticateWithFacebook: function(provider) 
    {
           var self = this;
           this.get('session').authenticate('simple-auth-authenticator:torii', "facebook-connect" ).then(function() 
    {

              var access_token= self.get('session.accessToken');

              Ember.$.ajax({
                type: 'POST',
                url: 'http://localhost:3000/users/auth/facebook_access_token/callback',

                dataType: 'json',
                data: {
                  access_token : access_token,

                },
                success: function(data, textStatus, jqXHR) {
                  // Handle success case
                },
                error: function(jqXHR, textStatus, errorThrown) {
                  // Handle error case
                }
              });

              self.transitionTo('dashboard');
             });
        },
于 2015-05-26T17:08:28.320 回答