-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_access.py
42 lines (34 loc) · 1.07 KB
/
api_access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import requests, base64, json, sys
import config
def get_access_token():
kwargs = {"grant_type": "client_credentials"}
req = requests.post(
config.REFRESH_TOKEN_URL,
data=kwargs,
headers={
"Authorization": "Basic "
+ base64.b64encode(
bytes(config.CONSUMER_KEY + ":" + config.CONSUMER_SECRET, "utf-8")
).decode("utf-8")
},
)
text = req.text
response = json.loads(text)
config.ACCESS_TOKEN = response["access_token"]
def check_is_undergrad(username):
# get an updated access token
get_access_token()
# query for information
kwargs = {"uid": username}
req = requests.get(
config.DIRECTORY_BASE_URL + config.USERS,
params=kwargs if "kwargs" not in kwargs else kwargs["kwargs"],
headers={"Authorization": "Bearer " + config.ACCESS_TOKEN},
)
json = req.json()
if len(json) == 0:
return False
return True
if __name__ == "__main__":
is_undergrad = check_is_undergrad(sys.argv[1])
print(sys.argv[1], is_undergrad)