-
Notifications
You must be signed in to change notification settings - Fork 0
/
mal.py
73 lines (52 loc) · 2.25 KB
/
mal.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import requests
import secrets
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT SECRET'
# 1. Generate a new Code Verifier / Code Challenge.
def get_new_code_verifier() -> str:
token = secrets.token_urlsafe(100)
return token[:128]
# 2. Print the URL needed to authorise your application.
def print_new_authorisation_url(code_challenge: str):
global CLIENT_ID
url = f'https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id={CLIENT_ID}&code_challenge={code_challenge}'
print(f'Authorise your application by clicking here: {url}\n')
# 3. Once you've authorised your application, you will be redirected to the webpage you've
# specified in the API panel. The URL will contain a parameter named "code" (the Authorisation
# Code). You need to feed that code to the application.
def generate_new_token(authorisation_code: str, code_verifier: str) -> dict:
global CLIENT_ID, CLIENT_SECRET
url = 'https://myanimelist.net/v1/oauth2/token'
data = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': authorisation_code,
'code_verifier': code_verifier,
'grant_type': 'authorization_code'
}
response = requests.post(url, data)
response.raise_for_status() # Check whether the request contains errors
token = response.json()
response.close()
print('Token generated successfully!')
with open('token.json', 'w') as file:
json.dump(token, file, indent = 4)
print('Token saved in "token.json"')
return token
# 4. Test the API by requesting your profile information
def print_user_info(access_token: str):
url = 'https://api.myanimelist.net/v2/users/@me'
response = requests.get(url, headers = {
'Authorization': f'Bearer {access_token}'
})
response.raise_for_status()
user = response.json()
response.close()
print(f"\n>>> Greetings {user['name']}! <<<")
if __name__ == '__main__':
code_verifier = code_challenge = get_new_code_verifier()
print_new_authorisation_url(code_challenge)
authorisation_code = input('Copy-paste the Authorisation Code: ').strip()
token = generate_new_token(authorisation_code, code_verifier)
print_user_info(token['access_token'])