facebook.rb 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = []
  48. client.get_connections('me', 'accounts').each do |page|
  49. pages.push(
  50. id: page['id'],
  51. name: page['name'],
  52. access_token: page['access_token'],
  53. )
  54. end
  55. # check if account already exists
  56. Channel.where(area: 'Facebook::Account').each do |channel|
  57. next if !channel.options
  58. next if !channel.options['user']
  59. next if !channel.options['user']['id']
  60. next if channel.options['user']['id'] != user['id']
  61. channel.options['auth']['access_token'] = access_token
  62. channel.options['pages'] = pages
  63. channel.save!
  64. return channel
  65. end
  66. # create channel
  67. Channel.create!(
  68. area: 'Facebook::Account',
  69. options: {
  70. adapter: 'facebook',
  71. auth: {
  72. access_token: access_token
  73. },
  74. user: user,
  75. pages: pages,
  76. sync: {
  77. pages: [],
  78. }
  79. },
  80. active: true,
  81. created_by_id: 1,
  82. updated_by_id: 1,
  83. )
  84. end
  85. end