diff --git a/kwoc/app.py b/kwoc/app.py index 58fb1ac..b8dae1d 100644 --- a/kwoc/app.py +++ b/kwoc/app.py @@ -4,9 +4,11 @@ import sys import os import json -from flask import render_template, redirect, Markup +from flask import render_template, redirect, Markup, request import markdown -from . import config +import config +import oauth +import requests as rq sys.path.append("kwoc") @@ -16,6 +18,7 @@ # Load stats.json file dir_path = os.path.dirname(os.path.realpath(__file__)) root_dir = '/'.join(dir_path.split('/')[:-1]) + stats_json = root_dir + '/gh_scraper/stats/stats.json' with open(stats_json, 'r') as f: stats_dict = json.load(f) @@ -42,7 +45,6 @@ # Final data stats_dict = non_zero_contributions - # Define routes @app.route("/") def main(): @@ -204,6 +206,21 @@ def summit_talkid(talk_id): else: return redirect('/summit', code=302) +@app.route("/auth/") +def auth(githubHandle): + global user + user = githubHandle + return redirect(oauth.ret_auth_url()) + +@app.route("/token") +def token(): + code=request.args.get('code') + access_token = oauth.ret_token(code) + print(access_token) + # user = githubhandle, accesstoken = access_token + return redirect("/") + + # # Lines below should not be needed for Python 3 # from imp import reload # reload(sys) diff --git a/kwoc/oauth.py b/kwoc/oauth.py new file mode 100644 index 0000000..fe8ceb1 --- /dev/null +++ b/kwoc/oauth.py @@ -0,0 +1,25 @@ +import requests as rq + +CLIENT_ID = "f50844d4efc205823232" +CLIENT_SECRET = "7aa71b9a99ba2f325d76e68659c51a2368166a8f" + +def ret_auth_url(): + auth_url = "https://github.com/login/oauth/authorize" + SCOPES = ["user"] + + url = auth_url+'?'+"client_id="+CLIENT_ID+'&'+'scope='+SCOPES[0] + if len(SCOPES) > 1: + for scope in SCOPES[1:]: + url += '+'+scope + + url+= '&access_type=offline&response_type=code' + return url + +def ret_token(code): + token_url = "https://github.com/login/oauth/access_token" + payload = {} + payload["client_id"], payload["client_secret"],payload["code"] = CLIENT_ID, CLIENT_SECRET, code + return rq.post(token_url,payload).content.decode().split("&")[0].split("=")[-1] + +if __name__ == '__main__': + print(ret_auth_url()) \ No newline at end of file