Skip to content
This repository was archived by the owner on Jan 11, 2022. It is now read-only.

Commit 0f726e3

Browse files
committed
auth: create_token fn to create token for gistey
Closes #44
1 parent 8305354 commit 0f726e3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

gistey/auth.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
import datetime
3+
import time
4+
import getpass
5+
from functools import partial
6+
7+
end = "https://api.github.com/"
8+
9+
10+
def authorize(func):
11+
"""
12+
Authorize application
13+
14+
:param func:
15+
function to decorate for required authorization.
16+
"""
17+
18+
username = input('Enter GitHub Username: ')
19+
password = getpass.getpass()
20+
return partial(func, auth=(username, password))
21+
22+
23+
@authorize
24+
def create_token(end, auth):
25+
"""
26+
Get the Personal access token or create one if not exists already.
27+
28+
:param end:
29+
End point of the API.
30+
:param auth:
31+
A tuple consisting of username and password
32+
"""
33+
34+
params = {
35+
"scope": ["gist"],
36+
"note": "gistey"
37+
}
38+
39+
rq = requests.post(end + "authorizations/", params=params, auth=auth)
40+
41+
if "X-GitHub-OTP" in rq.headers:
42+
otp = input("Enter 2FA code: ")
43+
rq = requests.put(end + "authorizations/", params=params, auth=auth,
44+
headers={"X-GitHub-OTP": otp})
45+
46+
return rq
47+
# TODO Returns 404 =/ Confusing

0 commit comments

Comments
 (0)