2022-01-24 19:37:27 +01:00
|
|
|
from requests_oauthlib import OAuth1Session
|
|
|
|
from flask import Blueprint, Response, current_app, request, jsonify
|
2022-01-24 20:30:06 +01:00
|
|
|
from ayct_backend.twitter.models import *
|
2022-01-31 18:29:20 +01:00
|
|
|
from ayct_backend.firebase import verify_token
|
2022-01-24 19:37:27 +01:00
|
|
|
|
|
|
|
twitter = Blueprint('twitter', __name__)
|
|
|
|
|
|
|
|
@twitter.route('/account', methods=['GET'])
|
|
|
|
def get_twitter_accounts():
|
2022-01-31 18:29:20 +01:00
|
|
|
decoded_token = verify_token(request.headers)
|
|
|
|
if not decoded_token:
|
|
|
|
return "Not authorised!", 401
|
|
|
|
|
2022-01-31 19:05:14 +01:00
|
|
|
user_id = decoded_token['sub']
|
2022-01-31 18:29:20 +01:00
|
|
|
|
|
|
|
twitter_accounts = TwitterAccount.query.filter_by(user_id=user_id)
|
2022-01-24 19:37:27 +01:00
|
|
|
|
|
|
|
accounts = []
|
|
|
|
|
|
|
|
for account in twitter_accounts:
|
|
|
|
account_data = {}
|
2022-01-24 23:26:55 +01:00
|
|
|
account_data['twitter_account_id'] = account.twitter_account_id
|
2022-01-24 19:37:27 +01:00
|
|
|
account_data['username'] = account.username
|
|
|
|
accounts.append(account_data)
|
|
|
|
|
2022-01-24 22:20:54 +01:00
|
|
|
return jsonify({
|
|
|
|
"twitter_accounts": accounts
|
|
|
|
}), 200
|
2022-01-24 19:37:27 +01:00
|
|
|
|
|
|
|
@twitter.route('/account', methods=['POST'])
|
|
|
|
def add_twitter_account():
|
2022-01-31 18:29:20 +01:00
|
|
|
decoded_token = verify_token(request.headers)
|
|
|
|
if not decoded_token:
|
|
|
|
return "Not authorised!", 401
|
|
|
|
|
2022-01-31 19:05:14 +01:00
|
|
|
user_id = decoded_token['sub']
|
2022-01-31 18:29:20 +01:00
|
|
|
|
2022-01-24 19:37:27 +01:00
|
|
|
content_type = request.headers.get('Content-Type')
|
|
|
|
if (content_type == 'application/json'):
|
|
|
|
json = request.json
|
2022-01-24 21:17:01 +01:00
|
|
|
if not json['veryfier'] or not json['oauth_token'] or not json['oauth_token_secret']:
|
2022-01-24 22:20:54 +01:00
|
|
|
return "Invalid request!", 400
|
2022-01-24 19:37:27 +01:00
|
|
|
else:
|
2022-01-24 20:34:03 +01:00
|
|
|
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
|
|
|
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
|
|
|
|
2022-01-24 19:37:27 +01:00
|
|
|
access_token_url = "https://api.twitter.com/oauth/access_token"
|
|
|
|
oauth = OAuth1Session(
|
|
|
|
consumer_key,
|
|
|
|
client_secret=consumer_secret,
|
2022-01-24 21:17:01 +01:00
|
|
|
resource_owner_key=json['oauth_token'],
|
|
|
|
resource_owner_secret=json['oauth_token_secret'],
|
2022-01-24 19:37:27 +01:00
|
|
|
verifier=json['veryfier'],
|
|
|
|
)
|
|
|
|
oauth_tokens = oauth.fetch_access_token(access_token_url)
|
|
|
|
|
|
|
|
new_twitter_account = TwitterAccount(
|
2022-01-31 19:58:11 +01:00
|
|
|
user_id = user_id,
|
2022-01-24 23:20:49 +01:00
|
|
|
twitter_account_id = oauth_tokens['user_id'],
|
|
|
|
username = oauth_tokens['screen_name'],
|
|
|
|
access_token = oauth_tokens['oauth_token'],
|
|
|
|
access_token_secret = oauth_tokens['oauth_token_secret']
|
2022-01-24 19:37:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
twitter_db.session.add(new_twitter_account)
|
|
|
|
twitter_db.session.commit()
|
2022-01-24 22:20:54 +01:00
|
|
|
|
|
|
|
return "Twitter account succesfully added.", 201
|
2022-01-24 19:37:27 +01:00
|
|
|
else:
|
2022-01-24 22:20:54 +01:00
|
|
|
return "Content-type not supported!", 400
|