facebook.rb 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ExternalCredential::Facebook
  3. def self.app_verify(params)
  4. request_account_to_link(params, false)
  5. params
  6. end
  7. def self.request_account_to_link(credentials = {}, app_required = true)
  8. external_credential = ExternalCredential.find_by(name: 'facebook')
  9. raise Exceptions::UnprocessableEntity, __('No Facebook app configured!') if !external_credential && app_required
  10. if external_credential
  11. if credentials[:application_id].blank?
  12. credentials[:application_id] = external_credential.credentials['application_id']
  13. end
  14. if credentials[:application_secret].blank?
  15. credentials[:application_secret] = external_credential.credentials['application_secret']
  16. end
  17. end
  18. raise Exceptions::UnprocessableEntity, __("The required parameter 'application_id' is missing.") if credentials[:application_id].blank?
  19. raise Exceptions::UnprocessableEntity, __("The required parameter 'application_secret' is missing.") if credentials[:application_secret].blank?
  20. oauth = Koala::Facebook::OAuth.new(
  21. credentials[:application_id],
  22. credentials[:application_secret],
  23. ExternalCredential.callback_url('facebook'),
  24. )
  25. oauth.get_app_access_token.inspect
  26. state = SecureRandom.uuid
  27. {
  28. request_token: state,
  29. # authorize_url: oauth.url_for_oauth_code(permissions: 'publish_pages, manage_pages, user_posts', state: state),
  30. # authorize_url: oauth.url_for_oauth_code(permissions: 'publish_pages, manage_pages', state: state),
  31. authorize_url: oauth.url_for_oauth_code(permissions: 'pages_manage_posts, pages_manage_engagement, pages_manage_metadata, pages_read_engagement, pages_read_user_content', state: state),
  32. }
  33. end
  34. def self.link_account(_request_token, params)
  35. # fail if request_token.params[:oauth_token] != params[:state]
  36. external_credential = ExternalCredential.find_by(name: 'facebook')
  37. raise Exceptions::UnprocessableEntity, __('No Facebook app configured!') if !external_credential
  38. oauth = Koala::Facebook::OAuth.new(
  39. external_credential.credentials['application_id'],
  40. external_credential.credentials['application_secret'],
  41. ExternalCredential.callback_url('facebook'),
  42. )
  43. access_token = oauth.get_access_token(params[:code])
  44. client = Koala::Facebook::API.new(access_token)
  45. user = client.get_object('me')
  46. # p client.get_connections('me', 'accounts').inspect
  47. pages = client.get_connections('me', 'accounts').map do |page|
  48. {
  49. id: page['id'],
  50. name: page['name'],
  51. access_token: page['access_token']
  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. pages: [],
  77. }
  78. },
  79. active: true,
  80. created_by_id: 1,
  81. updated_by_id: 1,
  82. )
  83. end
  84. end