2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-12-30 13:24:13 +00:00
|
|
|
class ExternalCredential::Facebook
|
|
|
|
|
|
|
|
def self.app_verify(params)
|
2018-12-03 14:10:36 +00:00
|
|
|
request_account_to_link(params, false)
|
2015-12-30 13:24:13 +00:00
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
def self.request_account_to_link(credentials = {}, app_required = true)
|
2015-12-30 13:24:13 +00:00
|
|
|
external_credential = ExternalCredential.find_by(name: 'facebook')
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('No facebook app configured!') if !external_credential && app_required
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-12-03 14:10:36 +00:00
|
|
|
if external_credential
|
|
|
|
if credentials[:application_id].blank?
|
|
|
|
credentials[:application_id] = external_credential.credentials['application_id']
|
|
|
|
end
|
|
|
|
if credentials[:application_secret].blank?
|
|
|
|
credentials[:application_secret] = external_credential.credentials['application_secret']
|
|
|
|
end
|
2015-12-30 13:24:13 +00:00
|
|
|
end
|
2018-12-03 14:10:36 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('No application_id param!') if credentials[:application_id].blank?
|
|
|
|
raise Exceptions::UnprocessableEntity, __('No application_secret param!') if credentials[:application_secret].blank?
|
2018-12-03 14:10:36 +00:00
|
|
|
|
2015-12-30 13:24:13 +00:00
|
|
|
oauth = Koala::Facebook::OAuth.new(
|
|
|
|
credentials[:application_id],
|
|
|
|
credentials[:application_secret],
|
|
|
|
ExternalCredential.callback_url('facebook'),
|
|
|
|
)
|
|
|
|
oauth.get_app_access_token.inspect
|
2021-09-20 10:47:05 +00:00
|
|
|
state = SecureRandom.uuid
|
2015-12-30 13:24:13 +00:00
|
|
|
{
|
|
|
|
request_token: state,
|
2021-07-16 13:44:10 +00:00
|
|
|
# authorize_url: oauth.url_for_oauth_code(permissions: 'publish_pages, manage_pages, user_posts', state: state),
|
|
|
|
# authorize_url: oauth.url_for_oauth_code(permissions: 'publish_pages, manage_pages', state: state),
|
2021-03-16 15:24:09 +00:00
|
|
|
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),
|
2015-12-30 13:24:13 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.link_account(_request_token, params)
|
|
|
|
# fail if request_token.params[:oauth_token] != params[:state]
|
|
|
|
external_credential = ExternalCredential.find_by(name: 'facebook')
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('No facebook app configured!') if !external_credential
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-30 13:24:13 +00:00
|
|
|
oauth = Koala::Facebook::OAuth.new(
|
|
|
|
external_credential.credentials['application_id'],
|
|
|
|
external_credential.credentials['application_secret'],
|
|
|
|
ExternalCredential.callback_url('facebook'),
|
|
|
|
)
|
|
|
|
|
|
|
|
access_token = oauth.get_access_token(params[:code])
|
|
|
|
client = Koala::Facebook::API.new(access_token)
|
|
|
|
user = client.get_object('me')
|
2021-07-16 13:44:10 +00:00
|
|
|
# p client.get_connections('me', 'accounts').inspect
|
2015-12-30 13:24:13 +00:00
|
|
|
pages = []
|
2017-10-01 12:25:52 +00:00
|
|
|
client.get_connections('me', 'accounts').each do |page|
|
2015-12-30 13:24:13 +00:00
|
|
|
pages.push(
|
|
|
|
id: page['id'],
|
|
|
|
name: page['name'],
|
|
|
|
access_token: page['access_token'],
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-30 13:24:13 +00:00
|
|
|
|
|
|
|
# check if account already exists
|
2017-10-01 12:25:52 +00:00
|
|
|
Channel.where(area: 'Facebook::Account').each do |channel|
|
2015-12-30 13:24:13 +00:00
|
|
|
next if !channel.options
|
|
|
|
next if !channel.options['user']
|
|
|
|
next if !channel.options['user']['id']
|
|
|
|
next if channel.options['user']['id'] != user['id']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-12-30 13:24:13 +00:00
|
|
|
channel.options['auth']['access_token'] = access_token
|
|
|
|
channel.options['pages'] = pages
|
2018-12-03 14:10:36 +00:00
|
|
|
channel.save!
|
2015-12-30 13:24:13 +00:00
|
|
|
return channel
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-30 13:24:13 +00:00
|
|
|
|
|
|
|
# create channel
|
2018-12-03 14:10:36 +00:00
|
|
|
Channel.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
area: 'Facebook::Account',
|
|
|
|
options: {
|
2015-12-30 13:24:13 +00:00
|
|
|
adapter: 'facebook',
|
2018-12-19 17:31:51 +00:00
|
|
|
auth: {
|
2015-12-30 13:24:13 +00:00
|
|
|
access_token: access_token
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
user: user,
|
|
|
|
pages: pages,
|
|
|
|
sync: {
|
2015-12-30 13:24:13 +00:00
|
|
|
pages: [],
|
|
|
|
}
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2015-12-30 13:24:13 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|