Python 3 Flask Github API Example to Search Information by Username in Browser Full Example For Beginners



pip install flask



pip install flask-github



Now make an file and copy paste the following code



    GitHub Example

    Shows how to authorize users with Github.

from flask import Flask, request, g, session, redirect, url_for
from flask import render_template_string, jsonify
from flask_github import GitHub

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

DATABASE_URI = 'sqlite:////tmp/github-flask.db'
SECRET_KEY = 'development key'
DEBUG = True

# Set these values

# setup flask
app = Flask(__name__)

# setup github-flask
github = GitHub(app)

# setup sqlalchemy
engine = create_engine(app.config['DATABASE_URI'])
db_session = scoped_session(sessionmaker(autocommit=False,
Base = declarative_base()
Base.query = db_session.query_property()

def init_db():

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    github_access_token = Column(String(255))
    github_id = Column(Integer)
    github_login = Column(String(255))

    def __init__(self, github_access_token):
        self.github_access_token = github_access_token

def before_request():
    g.user = None
    if 'user_id' in session:
        g.user = User.query.get(session['user_id'])

def after_request(response):
    return response

def index():
    if g.user:
        t = 'Hello! %s <a href="{{ url_for("user") }}">Get user</a> ' \
            '<a href="{{ url_for("repo") }}">Get repo</a> ' \
            '<a href="{{ url_for("logout") }}">Logout</a>'
        t %= g.user.github_login
        t = 'Hello! <a href="{{ url_for("login") }}">Login</a>'

    return render_template_string(t)

def token_getter():
    user = g.user
    if user is not None:
        return user.github_access_token

def authorized(access_token):
    next_url = request.args.get('next') or url_for('index')
    if access_token is None:
        return redirect(next_url)

    user = User.query.filter_by(github_access_token=access_token).first()
    if user is None:
        user = User(access_token)

    user.github_access_token = access_token

    # Not necessary to get these details here
    # but it helps humans to identify users easily.
    g.user = user
    github_user = github.get('/user')
    user.github_id = github_user['id']
    user.github_login = github_user['login']


    session['user_id'] =
    return redirect(next_url)

def login():
    if session.get('user_id', None) is None:
        return github.authorize()
        return 'Already logged in'

def logout():
    session.pop('user_id', None)
    return redirect(url_for('index'))

def user():
    return jsonify(github.get('/user'))

def repo():
    return jsonify(github.get('/repos/cenkalti/github-flask'))

if __name__ == '__main__':




Here just replace the github client id and secret inside the code

Leave a Reply