Skip to content
Permalink
Browse files

LBSD-2263 Moving back to use flow and oauth2 mode from yt

  • Loading branch information...
eos87 committed Feb 22, 2019
1 parent 571520b commit 980c6811f38b66cc70d65e741e48d3a12aad2822
@@ -747,11 +747,14 @@ export default function BlogEditController(
upload.start({
method: 'POST',
url: `${config.server.url}/video_upload/credential`,
data: {secretsFile},
data: {
secretsFile: secretsFile,
currentUrl: window.location.href,
},
}).then((response) => {
console.log(response); // eslint-disable-line
notify.pop();
notify.info(gettext('Saved credentials'));
window.location.replace(response.data);
});
}, () => cleanEditor(true));
},
@@ -1,4 +1,4 @@
#rest: gunicorn -c gunicorn_config.py wsgi --reload
rest: gunicorn -c gunicorn_config.py wsgi --reload
wamp: python3 -u ws.py
work: celery -A worker worker
beat: celery -A worker beat --pid=
@@ -9,13 +9,15 @@
# at https://www.sourcefabric.org/superdesk/license


import logging
import superdesk
from liveblog.validator import LiveblogValidator
from superdesk import get_backend, get_resource_service
from superdesk.resource import Resource
from superdesk.services import BaseService

_preferences_key = 'global_preferences'
logger = logging.getLogger('superdesk.liveblog.preferences')


def init_app(app):
@@ -42,6 +44,21 @@ class GlobalPreferencesResource(Resource):


class GlobalPreferencesService(BaseService):

def save_preference(self, key, data):
global_prefs = self.get_global_prefs()

try:
if key in global_prefs:
setting = self.get(req=None, lookup={'key': key})[0]
self.system_update(setting['_id'], {'value': data}, setting)
else:
self.post([{'key': key, 'value': data}])
return True
except Exception as err:
logger.error('Unable to save preference {0}. Exception msg: {1}'.format(key, err))
return False

def get_global_prefs(self):
res = get_resource_service(_preferences_key).get(req=None, lookup={})
return dict([v['key'], v['value']] for v in res if 'value' in v and 'key' in v)
@@ -2,16 +2,21 @@
import logging
import os

import six
import flask
import httplib2

from google_auth_oauthlib.flow import Flow

from oauth2client import _helpers
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

from flask import current_app as app
from flask import Blueprint, make_response, request
from flask import Blueprint, make_response, request, session
from flask_cors import CORS

from superdesk import get_resource_service
from superdesk.resource import Resource
from superdesk.services import BaseService

@@ -27,6 +32,8 @@
# set this only when in local server
# os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
CLIENT_SECRETS_FILE = 'youtube/client-secret.json'
YT_KEY = 'youtube_secrets'
YT_CREDENTIALS = 'youtube_credentials'

SCOPES = ['https://www.googleapis.com/auth/youtube']
API_SERVICE_NAME = 'youtube'
@@ -78,6 +85,25 @@ def fileExists(filename):
return app.media.exists({'filename': filename})


def bytes2string(value):
"""Converts bytes to a string value, if necessary.
Args:
value: The string/bytes value to be converted.
Returns:
The original value converted to unicode (if bytes) or as passed in
if it started out as unicode.
Raises:
ValueError if the value could not be converted to unicode.
"""
result = value.decode('utf-8') if isinstance(value, six.binary_type) else value

if isinstance(result, six.text_type):
return result
else:
raise ValueError(
'{0!r} could not be converted to unicode'.format(value))


@video_upload_blueprint.route('/api/video_upload/token', methods=['GET'])
def get_token():
if (fileExists(CLIENT_SECRETS_FILE)):
@@ -95,39 +121,57 @@ def get_token():
return api_error('Missing youtube credentials', 501)


@video_upload_blueprint.route('/api/video_upload/credential', methods=['POST', 'GET'])
@video_upload_blueprint.route('/api/video_upload/credential', methods=['POST'])
def get_refresh_token():
# TODO: add condition, file must be provided
secrets = request.files['secretsFile']
# let's save current url to later redirect
session['current_url'] = request.values['currentUrl']

secrets = request.files['secretsFile']
secrets.seek(0)
file_content = secrets.read()
yt_data = json.loads(bytes2string(file_content))

secrets_file = app.media.put(
file_content, filename=CLIENT_SECRETS_FILE, content_type='application/json', version=False)
if 'web' not in yt_data:
return api_error('OAuth project has to be configured as web in google console', 400)

return api_response({'_id': secrets_file}, 200)
# let's save secrets file content in db for future usage
global_serv = get_resource_service('global_preferences')
global_serv.save_preference(YT_KEY, yt_data)

redirect_uri = flask.url_for(
'video_upload.oauth2callback', _external=True, _scheme='https')

flow = Flow.from_client_config(
yt_data, scopes=SCOPES, redirect_uri=redirect_uri)
auth_url, _ = flow.authorization_url(
prompt='consent', access_type='offline', include_granted_scopes='true')

return make_response(auth_url, 200)


@video_upload_blueprint.route('/api/video_upload/oauth2callback', methods=['GET', 'POST'])
def oauth2callback():
# flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
# CLIENT_SECRETS_FILE, scopes=SCOPES)
# flow.redirect_uri = flask.url_for('video_upload.oauth2callback', _external=True, _scheme='https')

# # Use the authorization server's response to fetch the OAuth 2.0 tokens.
# authorization_response = flask.request.url
# authorization_response = authorization_response.replace('http', 'https')
# flow.fetch_token(authorization_response=authorization_response)

# credentials = flow.credentials
# client = app.data.mongo.pymongo('video_upload').db['video_upload']
# current_url_data = client.find_one()
# current_url = current_url_data['current_url']
# client.update({}, {'client_id': credentials.client_id, 'client_secret': credentials.client_secret,
# 'refresh_token': credentials.refresh_token}, True)
return ""
# return flask.redirect(current_url)

yt_prefs = get_resource_service('global_preferences').get_global_prefs()[YT_KEY]
yt_data = yt_prefs['value']

redirect_uri = flask.url_for('video_upload.oauth2callback', _external=True, _scheme='https')
flow = Flow.from_client_config(yt_data, scopes=SCOPES, redirect_uri=redirect_uri)

# Use the authorization server's response to fetch the OAuth 2.0 tokens.
authorization_response = flask.request.url
authorization_response = authorization_response.replace('http', 'https')
flow.fetch_token(authorization_response=authorization_response)

credentials = flow.credentials
global_serv = get_resource_service('global_preferences')
global_serv.save_preference(YT_CREDENTIALS, {
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'refresh_token': credentials.refresh_token
})

return flask.redirect(session['current_url'])


@video_upload_blueprint.route('/api/video_upload/callback_url', methods=['GET'])

0 comments on commit 980c681

Please sign in to comment.
You can’t perform that action at this time.