facebook.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. class ExternalCredential::Facebook
  2. def self.app_verify(params)
  3. request_account_to_link(params, false)
  4. params
  5. end
  6. def self.request_account_to_link(credentials = {}, app_required = true)
  7. external_credential = ExternalCredential.find_by(name: 'facebook')
  8. raise Exceptions::UnprocessableEntity, 'No facebook app configured!' if !external_credential && app_required
  9. if external_credential
  10. if credentials[:application_id].blank?
  11. credentials[:application_id] = external_credential.credentials['application_id']
  12. end
  13. if credentials[:application_secret].blank?
  14. credentials[:application_secret] = external_credential.credentials['application_secret']
  15. end
  16. end
  17. raise Exceptions::UnprocessableEntity, 'No application_id param!' if credentials[:application_id].blank?
  18. raise Exceptions::UnprocessableEntity, 'No application_secret param!' if credentials[:application_secret].blank?
  19. oauth = Koala::Facebook::OAuth.new(
  20. credentials[:application_id],
  21. credentials[:application_secret],
  22. ExternalCredential.callback_url('facebook'),
  23. )
  24. oauth.get_app_access_token.inspect
  25. state = rand(999_999_999_999).to_s
  26. {
  27. request_token: state,
  28. #authorize_url: oauth.url_for_oauth_code(permissions: 'publish_pages, manage_pages, user_posts', state: state),
  29. authorize_url: oauth.url_for_oauth_code(permissions: 'publish_pages, manage_pages', state: state),
  30. }
  31. end
  32. def self.link_account(_request_token, params)
  33. # fail if request_token.params[:oauth_token] != params[:state]
  34. external_credential = ExternalCredential.find_by(name: 'facebook')
  35. raise Exceptions::UnprocessableEntity, 'No facebook app configured!' if !external_credential
  36. oauth = Koala::Facebook::OAuth.new(
  37. external_credential.credentials['application_id'],
  38. external_credential.credentials['application_secret'],
  39. ExternalCredential.callback_url('facebook'),
  40. )
  41. access_token = oauth.get_access_token(params[:code])
  42. client = Koala::Facebook::API.new(access_token)
  43. user = client.get_object('me')
  44. #p client.get_connections('me', 'accounts').inspect
  45. pages = []
  46. client.get_connections('me', 'accounts').each do |page|
  47. pages.push(
  48. id: page['id'],
  49. name: page['name'],
  50. access_token: page['access_token'],
  51. perms: page['perms'],
  52. )
  53. end
  54. # check if account already exists
  55. Channel.where(area: 'Facebook::Account').each do |channel|
  56. next if !channel.options
  57. next if !channel.options['user']
  58. next if !channel.options['user']['id']
  59. next if channel.options['user']['id'] != user['id']
  60. channel.options['auth']['access_token'] = access_token
  61. channel.options['pages'] = pages
  62. channel.save!
  63. return channel
  64. end
  65. # create channel
  66. Channel.create!(
  67. area: 'Facebook::Account',
  68. options: {
  69. adapter: 'facebook',
  70. auth: {
  71. access_token: access_token
  72. },
  73. user: user,
  74. pages: pages,
  75. sync: {
  76. wall: {},
  77. pages: [],
  78. }
  79. },
  80. active: true,
  81. created_by_id: 1,
  82. updated_by_id: 1,
  83. )
  84. end
  85. end