| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount import app_settings |
| 4 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 5 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 6 |
from allauth.socialaccount.providers.spotify.views import SpotifyOAuth2Adapter |
| 7 |
|
| 8 |
|
| 9 |
class SpotifyAccount(ProviderAccount): |
| 10 |
def get_profile_url(self): |
| 11 |
return self.account.extra_data.get("external_urls").get("spotify") |
| 12 |
|
| 13 |
def get_avatar_url(self): |
| 14 |
try: |
| 15 |
return self.account.extra_data.get("images")[0].get("url") |
| 16 |
except IndexError: |
| 17 |
return None |
| 18 |
|
| 19 |
|
| 20 |
class SpotifyOAuth2Provider(OAuth2Provider): |
| 21 |
id = "spotify" |
| 22 |
name = "Spotify" |
| 23 |
account_class = SpotifyAccount |
| 24 |
oauth2_adapter_class = SpotifyOAuth2Adapter |
| 25 |
|
| 26 |
def extract_uid(self, data): |
| 27 |
return data["id"] |
| 28 |
|
| 29 |
def extract_common_fields(self, data): |
| 30 |
return dict(name=data.get("display_name"), email=data.get("email")) |
| 31 |
|
| 32 |
def get_default_scope(self): |
| 33 |
scope = [] |
| 34 |
if app_settings.QUERY_EMAIL: |
| 35 |
scope.append("user-read-email") |
| 36 |
return scope |
| 37 |
|
| 38 |
|
| 39 |
provider_classes = [SpotifyOAuth2Provider] |