ruby 手动OAuth2 facebook for Rails API的要点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 手动OAuth2 facebook for Rails API的要点相关的知识,希望对你有一定的参考价值。

# lib/omniauth/response_error.rb
module Omniauth
  class ResponseError < StandardError
  end
end
# lib/omniauth/permission_error.rb
module Omniauth
  class PermissionError < StandardError
  end
end
# lib/omniauth/facebook.rb

require 'httparty'

module Omniauth
  class Facebook
    include HTTParty

    # The base uri for facebook graph API
    base_uri 'https://graph.facebook.com/v2.3'

    # Used to authenticate app with facebook user
    # Usage
    #   Omniauth::Facebook.authenticate('authorization_code')
    # Flow
    #   Retrieve access_token from authorization_code
    #   Retrieve User_Info hash from access_token
    def self.authenticate(code)
      provider = self.new
      access_token = provider.get_access_token(code)
      user_info    = provider.get_user_profile(access_token)
      return user_info, access_token
    end

    # Used to revoke the application permissions and login if a user
    # revoked some of the mandatory permissions required by the application
    # like the email
    # Usage
    #    Omniauth::Facebook.deauthorize('user_id')
    # Flow
    #   Send DELETE /me/permissions?access_token=XXX
    def self.deauthorize(access_token)
      options  = { query: { access_token: access_token } }
      response = self.delete('/me/permissions', options)

      # Something went wrong most propably beacuse of the connection.
      unless response.success?
        Rails.logger.error 'Omniauth::Facebook.deauthorize Failed'
        fail Omniauth::ResponseError, 'errors.auth.facebook.deauthorization'
      end
      response.parsed_response
    end

    def get_access_token(code)
      response = self.class.get('/oauth/access_token', query(code))

      # Something went wrong either wrong configuration or connection
      unless response.success?
        Rails.logger.error 'Omniauth::Facebook.get_access_token Failed'
        fail Omniauth::ResponseError, 'errors.auth.facebook.access_token'
      end
      response.parsed_response['access_token']
    end

    def get_user_profile(access_token)
      options = { query: { access_token: access_token } }
      response = self.class.get('/me', options)

      # Something went wrong most propably beacuse of the connection.
      unless response.success?
        Rails.logger.error 'Omniauth::Facebook.get_user_profile Failed'
        fail Omniauth::ResponseError, 'errors.auth.facebook.user_profile'
      end
      response.parsed_response
    end

    private

    # access_token required params
    # https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.3#confirm
    def query(code)
      {
        query: {
          code: code,
          redirect_uri: "http://localhost:9000/",
          client_id: ENV['FB_APP_ID'],
          client_secret: ENV['FB_APP_SECRET']
        }
      }
    end
  end
end

以上是关于ruby 手动OAuth2 facebook for Rails API的要点的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot OAuth2 手动创建新的 JWT 令牌

Facebook Oauth2.0 API调用方法

OAuth2“社交登录”流程(允许通过 Facebook/Twitter 进行 OAuth2 身份验证):是不是有任何示例/文献?

使用 Facebook iOS SDK 的 OAuth2 授权代码

ruby 命令行OAuth2测试程序

Rest,Spring 拥有 OAuth2 服务器 + OAuth2 提供商,如 Facebook、Google、Yahoo