Posted by Ady on Mon, September 5, 2016
I’m back. A lot of things have changed since I last wrote and one of that is my go-to language.
I immediately turned to Flask-OAuth to do the job, but it keeps on failing with:
SSLHandshakeError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
It seems to be a problem with httplib2.
After struggling for quite some time, I found Flask-OAuthlib that claims to be a replacement for the outdated Flask-Oauth. It worked like a charm.
GitLab’s documentation on consuming its OAuth2 is quite basic. Below is a basic implementation that works.
All you need to do is change the gitlab.example.com to your GitLab server, and add the consumer_key and consumer_secret. If successful, the main page will display a JSON with the logged on user’s details.
from flask import Flask, render_template, redirect, url_for, session, request, jsonify from flask_oauthlib.client import OAuth app = Flask(__name__) app.debug = True app.secret_key = 'development' oauth = OAuth(app) gitlab = oauth.remote_app('gitlab', base_url='https://gitlab.example.com/api/v3/', request_token_url=None, access_token_url='https://gitlab.example.com/oauth/token', authorize_url='https://gitlab.example.com/oauth/authorize', access_token_method='POST', consumer_key='', consumer_secret='' ) @app.route('/') def index(): if 'gitlab_token' in session: me = gitlab.get('user') return jsonify(me.data) return redirect(url_for('login')) @app.route('/login') def login(): return gitlab.authorize(callback=url_for('authorized', _external=True, _scheme='https')) @app.route('/logout') def logout(): del session['gitlab_token'] return redirect(url_for('index')) @app.route('/login/authorized') def authorized(): resp = gitlab.authorized_response() if resp is None: return 'Access denied: reason=%s error=%s' % ( request.args['error'], request.args['error_description'] ) session['gitlab_token'] = (resp['access_token'], '') return redirect(url_for('index')) @gitlab.tokengetter def get_gitlab_oauth_token(): return session.get('gitlab_token') if __name__ == "__main__": app.run()
I hope it saves someone some time.
Tue, March 24, 2015
Comments Off on CloudWatch INSUFFICIENT_DATA for Linux System Metric | Full article »
Thu, July 3, 2014
Comments Off on Why I Will Never Buy From Senheng Ever Again | Full article »
Mon, October 14, 2013
Comments Off on DD-WRT: OpenVPN Server Using Certificates | Full article »
Tue, August 13, 2013
Comments Off on WordPress Update: Upgrade package not available (3.5) | Full article »
Mon, May 27, 2013
Comments Off on Happy 10th Anniversary WordPress! | Full article »
Sat, April 20, 2013
Comments Off on CrashPlan 3.5.3 Headless Upgrade | Full article »
Mon, February 18, 2013
Comments Off on Inbox Zero: Gmail vs. Mailbox | Full article »
Mon, January 21, 2013
Comments Off on CloudFlare: Reliable? | Full article »
Sat, January 19, 2013
Comments Off on Redirecting WordPress Permalinks in Nginx | Full article »
Mon, October 1, 2012
Comments Off on Konvensyen Jutawan Awesome 2012 | Full article »
Tue, September 4, 2012
Comments Off on Pink iPhone Cable from Giveaway | Full article »
Sat, May 12, 2012
Comments Off on Sluggish iChat, Messages, Terminal, and Others in Mac OS X Lion | Full article »
Sat, May 5, 2012
1 Comment » | Full article »
Thu, February 23, 2012
Comments Off on UniFi Phone Call Forwarding | Full article »