2015-12-17 11:49:40 +00:00
|
|
|
class ExternalCredential::Twitter
|
|
|
|
|
|
|
|
def self.app_verify(params)
|
2015-12-30 13:24:13 +00:00
|
|
|
request_account_to_link(params)
|
|
|
|
params
|
2015-12-17 11:49:40 +00:00
|
|
|
end
|
|
|
|
|
2015-12-30 13:24:13 +00:00
|
|
|
def self.request_account_to_link(credentials = {})
|
2015-12-17 11:49:40 +00:00
|
|
|
external_credential = ExternalCredential.find_by(name: 'twitter')
|
2015-12-21 00:48:49 +00:00
|
|
|
if !credentials[:consumer_key]
|
|
|
|
credentials[:consumer_key] = external_credential.credentials['consumer_key']
|
|
|
|
end
|
|
|
|
if !credentials[:consumer_secret]
|
|
|
|
credentials[:consumer_secret] = external_credential.credentials['consumer_secret']
|
|
|
|
end
|
2015-12-17 11:49:40 +00:00
|
|
|
consumer = OAuth::Consumer.new(
|
2015-12-21 00:48:49 +00:00
|
|
|
credentials[:consumer_key],
|
|
|
|
credentials[:consumer_secret], {
|
2015-12-17 11:49:40 +00:00
|
|
|
site: 'https://api.twitter.com'
|
2015-12-30 13:24:13 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
request_token = consumer.get_request_token(oauth_callback: ExternalCredential.callback_url('twitter'))
|
2015-12-17 11:49:40 +00:00
|
|
|
{
|
|
|
|
request_token: request_token,
|
|
|
|
authorize_url: request_token.authorize_url,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.link_account(request_token, params)
|
2016-03-01 14:26:46 +00:00
|
|
|
raise if request_token.params[:oauth_token] != params[:oauth_token]
|
2015-12-17 11:49:40 +00:00
|
|
|
external_credential = ExternalCredential.find_by(name: 'twitter')
|
|
|
|
access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
|
|
|
|
client = Twitter::REST::Client.new(
|
|
|
|
consumer_key: external_credential.credentials[:consumer_key],
|
|
|
|
consumer_secret: external_credential.credentials[:consumer_secret],
|
|
|
|
access_token: access_token.token,
|
|
|
|
access_token_secret: access_token.secret,
|
|
|
|
)
|
|
|
|
user = client.user
|
|
|
|
|
2015-12-30 13:24:13 +00:00
|
|
|
# check if account already exists
|
2016-06-30 20:04:48 +00:00
|
|
|
Channel.where(area: 'Twitter::Account').each { |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']
|
|
|
|
|
|
|
|
# update access_token
|
|
|
|
channel.options['auth']['external_credential_id'] = external_credential.id
|
|
|
|
channel.options['auth']['oauth_token'] = access_token.token
|
|
|
|
channel.options['auth']['oauth_token_secret'] = access_token.secret
|
|
|
|
channel.save
|
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2015-12-17 11:49:40 +00:00
|
|
|
# create channel
|
|
|
|
Channel.create(
|
|
|
|
area: 'Twitter::Account',
|
|
|
|
options: {
|
|
|
|
adapter: 'twitter',
|
|
|
|
user: {
|
|
|
|
id: user.id,
|
|
|
|
screen_name: user.screen_name,
|
2015-12-21 00:48:49 +00:00
|
|
|
name: user.name,
|
2015-12-17 11:49:40 +00:00
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
external_credential_id: external_credential.id,
|
|
|
|
oauth_token: access_token.token,
|
|
|
|
oauth_token_secret: access_token.secret,
|
|
|
|
},
|
|
|
|
sync: {
|
2016-01-02 16:10:12 +00:00
|
|
|
limit: 20,
|
2015-12-17 11:49:40 +00:00
|
|
|
search: [],
|
|
|
|
mentions: {},
|
2017-01-19 15:23:41 +00:00
|
|
|
direct_messages: {},
|
|
|
|
track_retweets: false
|
2015-12-17 11:49:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
active: true,
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|