Rails Koala:GraphMethodException,代码:100,消息:API参数中提供了无效的appsecret_proof

时间:2018-08-13 23:21:35

标签: ruby-on-rails facebook facebook-graph-api hmac koala-gem

我正在Rails API上实现Facebook登录并正在使用Koala gem。

我在“用户”控制器中具有以下代码,并且即使不使用应用程序秘密密钥也能创建用户,即使在Facebook开发者控制台上,我也已指定“需要应用程序秘密:是”(也许会花费一些时间)才能使设置生效?)。

无论如何,当我尝试创建应用程序秘密密钥并将其传递给Koala :: Facebook :: API.new调用时,都会收到主题错误。

我在做什么错了?

 def facebook
    if params[:facebook_access_token]
      digest = OpenSSL::Digest.new('sha1')
      key = ENV["FB_SECRET"]
      data = params[:facebook_access_token]
      hmac = OpenSSL::HMAC.hexdigest(digest, key, data)

      # I have also tried different variations including hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, data)

      graph = Koala::Facebook::API.new(params[:facebook_access_token], hmac)

      user_data = graph.get_object("me?fields=name, email, id, picture")
      user = User.find_by email: user_data["email"]

      if user
        user.generate_new_authentication_token
        json_response "Logged in through Facebook successfully -- User information", true, {user: user}, :ok
      else
        user = User.new(email: user_data["email"],
                        uid: user_data["id"],
                        provider: "facebook",
                        image: user_data["picture"]["data"]["url"],
                        password: Devise.friendly_token[0,20])

        user.authentication_token = User.generate_unique_secure_token
        if user.save
          json_response "Created new user and logged in through Facebook successfully", true, {user: user}, :ok
        else
          json_response user.errors, false, {}, :unprocessable_entity
        end
      end
    else
      json_response "Missing Facebook access token", false, {}, :unprocessable_entity
    end
  end

0 个答案:

没有答案