-
-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathbeats.py
70 lines (61 loc) · 2.21 KB
/
beats.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
"""
Beats backend, docs at:
https://developer.beatsmusic.com/docs
"""
import base64
from ..utils import handle_http_errors
from .oauth import BaseOAuth2
class BeatsOAuth2(BaseOAuth2):
name = "beats"
SCOPE_SEPARATOR = " "
ID_KEY = "user_context"
AUTHORIZATION_URL = "https://partner.api.beatsmusic.com/v1/oauth2/authorize"
ACCESS_TOKEN_URL = "https://partner.api.beatsmusic.com/oauth2/token"
ACCESS_TOKEN_METHOD = "POST"
REDIRECT_STATE = False
def get_user_id(self, details, response):
return response["result"][BeatsOAuth2.ID_KEY]
def auth_headers(self):
return {
"Authorization": "Basic {}".format(
base64.urlsafe_b64encode(
"{}:{}".format(*self.get_key_and_secret()).encode()
)
)
}
@handle_http_errors
def auth_complete(self, *args, **kwargs):
"""Completes login process, must return user instance"""
self.process_error(self.data)
response = self.request_access_token(
self.ACCESS_TOKEN_URL,
data=self.auth_complete_params(self.validate_state()),
headers=self.auth_headers(),
method=self.ACCESS_TOKEN_METHOD,
)
self.process_error(response)
# mashery wraps in jsonrpc
if response.get("jsonrpc", None):
response = response.get("result", None)
return self.do_auth(
response["access_token"], response=response, *args, **kwargs
)
def get_user_details(self, response):
"""Return user details from Beats account"""
response = response["result"]
fullname, first_name, last_name = self.get_user_names(
response.get("display_name")
)
return {
"username": response.get("id"),
"email": response.get("email"),
"fullname": fullname,
"first_name": first_name,
"last_name": last_name,
}
def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
return self.get_json(
"https://partner.api.beatsmusic.com/v1/api/me",
headers={"Authorization": f"Bearer {access_token}"},
)