This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
from allauth.socialaccount import app_settings
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class SpotifyAccount(ProviderAccount):
def get_profile_url(self):
return self.account.extra_data.get("external_urls").get("spotify")
def get_avatar_url(self):
try:
return self.account.extra_data.get("images")[0].get("url")
except IndexError:
return None
def to_str(self):
dflt = super(SpotifyAccount, self).to_str()
return self.account.extra_data.get("display_name", dflt)
class SpotifyOAuth2Provider(OAuth2Provider):
id = "spotify"
name = "Spotify"
account_class = SpotifyAccount
def extract_uid(self, data):
return data["id"]
def extract_common_fields(self, data):
return dict(name=data.get("display_name"), email=data.get("email"))
def get_default_scope(self):
scope = []
if app_settings.QUERY_EMAIL:
scope.append("user-read-email")
return scope
provider_classes = [SpotifyOAuth2Provider]

View File

@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import SpotifyOAuth2Provider
class SpotifyOAuth2Tests(OAuth2TestsMixin, TestCase):
provider_id = SpotifyOAuth2Provider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""{
"birthdate": "1937-06-01",
"country": "SE",
"display_name": "JM Wizzler",
"email": "email@example.com",
"external_urls": {
"spotify": "https://open.spotify.com/user/wizzler"
},
"followers" : {
"href" : null,
"total" : 3829
},
"href": "https://api.spotify.com/v1/users/wizzler",
"id": "wizzler",
"images": [
{
"height": null,
"url":
"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc3/t1.0-1/1970403_10152215092574354_1798272330_n.jpg",
"width": null
}
],
"product": "premium",
"type": "user",
"uri": "spotify:user:wizzler"
}""",
) # noqa

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth.urls import default_urlpatterns
from .provider import SpotifyOAuth2Provider
urlpatterns = default_urlpatterns(SpotifyOAuth2Provider)

View File

@@ -0,0 +1,27 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import SpotifyOAuth2Provider
class SpotifyOAuth2Adapter(OAuth2Adapter):
provider_id = SpotifyOAuth2Provider.id
access_token_url = "https://accounts.spotify.com/api/token"
authorize_url = "https://accounts.spotify.com/authorize"
profile_url = "https://api.spotify.com/v1/me"
def complete_login(self, request, app, token, **kwargs):
extra_data = requests.get(
self.profile_url, params={"access_token": token.token}
)
return self.get_provider().sociallogin_from_response(request, extra_data.json())
oauth_login = OAuth2LoginView.adapter_view(SpotifyOAuth2Adapter)
oauth_callback = OAuth2CallbackView.adapter_view(SpotifyOAuth2Adapter)