def fconnect(): ''' Api for login using Facebook Authentications. ''' # loading code data = json.loads(request.data) access_token = data['authResponse']['accessToken'] provider_id = data['id'] # Avoid duplicated login stored_access_token = login_session.get('access_token') stored_provider_id = login_session.get('provider_id') if stored_access_token is not None and provider_id == stored_provider_id: return utilities.status(login_session.get('message'), 200, 'json') # Storing access token login_session['access_token'] = access_token # Check if the user exist user = models.select_user_by_email(data['email']) if user is None: user = models.insert_new_user(data['name'], data[ 'email'], data['picture']['data']['url'].strip('/')) login_session['provider_id'] = provider_id login_session['email'] = data['email'] login_session['message'] = {'picture': data['picture']['data']['url'].strip('/'), 'email': data[ 'email'], 'fullname': data['name'], 'id': user.id, 'provider': 'facebook'} return utilities.status(login_session.get('message'), 200, 'json')
def gconnect(): ''' Api for login using Goople Authentications. ''' # loading code code = request.data try: oauth_flow = flow_from_clientsecrets('client_secret.json', scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: print "Unexpected error:", sys.exc_info()[0] print "Unexpected error:", sys.exc_info()[1] return utilities.status('Failed to upgrade the authorization code', 401, 'json') except: print "Unexpected error:", sys.exc_info()[0] print "Unexpected error:", sys.exc_info()[1] abort(404) access_token = credentials.access_token provider_id = credentials.id_token['sub'] # Avoid duplicated login stored_access_token = login_session.get('access_token') stored_provider_id = login_session.get('provider_id') if stored_access_token is not None and provider_id == stored_provider_id: return utilities.status(login_session.get('message'), 200, 'json') # Storing access token login_session['access_token'] = access_token # Retrive user info for user userinfo_url = 'https://www.googleapis.com/oauth2/v2/userinfo' answer = requests.get(userinfo_url, headers={'Authorization': 'Bearer ' + access_token}) data = json.loads(answer.text) # Check if the user exist user = models.select_user_by_email(data['email']) if user is None: user = models.insert_new_user(data['name'], data['email'], data['picture'].strip('/')) login_session['provider_id'] = provider_id login_session['email'] = data['email'] login_session['message'] = { 'picture': data['picture'].strip('/'), 'email': data['email'], 'fullname': data['name'], 'id': user.id, 'provider': 'google' } return utilities.status(login_session.get('message'), 200, 'json')
def gconnect(): ''' Api for login using Goople Authentications. ''' # loading code code = request.data try: oauth_flow = flow_from_clientsecrets( 'client_secret.json', scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: print "Unexpected error:", sys.exc_info()[0] print "Unexpected error:", sys.exc_info()[1] return utilities.status('Failed to upgrade the authorization code', 401, 'json') except: print "Unexpected error:", sys.exc_info()[0] print "Unexpected error:", sys.exc_info()[1] abort(404) access_token = credentials.access_token provider_id = credentials.id_token['sub'] # Avoid duplicated login stored_access_token = login_session.get('access_token') stored_provider_id = login_session.get('provider_id') if stored_access_token is not None and provider_id == stored_provider_id: return utilities.status(login_session.get('message'), 200, 'json') # Storing access token login_session['access_token'] = access_token # Retrive user info for user userinfo_url = 'https://www.googleapis.com/oauth2/v2/userinfo' answer = requests.get(userinfo_url, headers={ 'Authorization': 'Bearer ' + access_token}) data = json.loads(answer.text) # Check if the user exist user = models.select_user_by_email(data['email']) if user is None: user = models.insert_new_user(data['name'], data[ 'email'], data['picture'].strip('/')) login_session['provider_id'] = provider_id login_session['email'] = data['email'] login_session['message'] = {'picture': data['picture'].strip('/'), 'email': data[ 'email'], 'fullname': data['name'], 'id': user.id, 'provider': 'google'} return utilities.status(login_session.get('message'), 200, 'json')
def fconnect(): ''' Api for login using Facebook Authentications. ''' # loading code data = json.loads(request.data) access_token = data['authResponse']['accessToken'] provider_id = data['id'] # Avoid duplicated login stored_access_token = login_session.get('access_token') stored_provider_id = login_session.get('provider_id') if stored_access_token is not None and provider_id == stored_provider_id: return utilities.status(login_session.get('message'), 200, 'json') # Storing access token login_session['access_token'] = access_token # Check if the user exist user = models.select_user_by_email(data['email']) if user is None: user = models.insert_new_user( data['name'], data['email'], data['picture']['data']['url'].strip('/')) login_session['provider_id'] = provider_id login_session['email'] = data['email'] login_session['message'] = { 'picture': data['picture']['data']['url'].strip('/'), 'email': data['email'], 'fullname': data['name'], 'id': user.id, 'provider': 'facebook' } return utilities.status(login_session.get('message'), 200, 'json')