This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
48 lines (42 loc) · 1.99 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import logging
from flask import Flask, jsonify, make_response, request, abort
from flask_httpauth import HTTPBasicAuth
from linkedin import linkedin
logging.basicConfig(filename = 'tweetsched-linkedin-publisher.log', level = logging.INFO)
auth = HTTPBasicAuth()
app = Flask(__name__)
@auth.get_password
def get_password(username):
if username == os.environ['SERVICE_KEY']:
return os.environ['SERVICE_PASS']
return None
@auth.error_handler
def unauthorized():
logging.info('Unauthorized access')
return make_response(jsonify({'error': 'Unauthorized access'}), 403)
@app.route('/api/v1/posts', methods = ['POST'])
@auth.login_required
def publish_post():
if not request.json or not 'profileId' or not 'message' in request.json:
logging.info('Not valid request')
abort(400)
authentication = linkedin.LinkedInDeveloperAuthentication(os.environ['CLIENT_KEY'],
os.environ['CLIENT_SECRET'],
os.environ['OAUTH_TOKEN'],
os.environ['OAUTH_SECRET'],
os.environ['RETURN_URL'],
linkedin.PERMISSIONS.enums.values())
application = linkedin.LinkedInApplication(authentication)
linkedinResponse = application.submit_share(request.json['message'], None, None, None, None)
if 'updateKey' in linkedinResponse and 'updateUrl' in linkedinResponse:
logging.info('Post was posted')
return jsonify({'status': 'Post was posted'}), 200
logging.error('Post was not posted. LinkedIn response: ' + linkedinResponse)
return jsonify({'status': 'Post was not posted'}), 502
@app.errorhandler(404)
@auth.login_required
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':
app.run(debug = False)