Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Set up OAuth for Github #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions kwoc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)
Expand All @@ -42,7 +45,6 @@
# Final data
stats_dict = non_zero_contributions


# Define routes
@app.route("/")
def main():
Expand Down Expand Up @@ -204,6 +206,21 @@ def summit_talkid(talk_id):
else:
return redirect('/summit', code=302)

@app.route("/auth/<githubHandle>")
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)
Expand Down
25 changes: 25 additions & 0 deletions kwoc/oauth.py
Original file line number Diff line number Diff line change
@@ -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())