| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.socialaccount.providers.oauth.client import OAuth |
| 6 |
from allauth.socialaccount.providers.oauth.views import ( |
| 7 |
OAuthAdapter, |
| 8 |
OAuthCallbackView, |
| 9 |
OAuthLoginView, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
API_BASE = "https://api.500px.com/v1" |
| 14 |
|
| 15 |
|
| 16 |
class FiveHundredPxAPI(OAuth): |
| 17 |
""" |
| 18 |
Verifying 500px credentials |
| 19 |
""" |
| 20 |
|
| 21 |
url = f"{API_BASE}/users" |
| 22 |
|
| 23 |
def get_user_info(self): |
| 24 |
return self.query(self.url).json()["user"] |
| 25 |
|
| 26 |
|
| 27 |
class FiveHundredPxOAuthAdapter(OAuthAdapter): |
| 28 |
provider_id = "500px" |
| 29 |
request_token_url = f"{API_BASE}/oauth/request_token" |
| 30 |
access_token_url = f"{API_BASE}/oauth/access_token" |
| 31 |
authorize_url = f"{API_BASE}/oauth/authorize" |
| 32 |
|
| 33 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 34 |
client = FiveHundredPxAPI( |
| 35 |
request, app.client_id, app.secret, self.request_token_url |
| 36 |
) |
| 37 |
extra_data = client.get_user_info() |
| 38 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 39 |
|
| 40 |
|
| 41 |
oauth_login = OAuthLoginView.adapter_view(FiveHundredPxOAuthAdapter) |
| 42 |
oauth_callback = OAuthCallbackView.adapter_view(FiveHundredPxOAuthAdapter) |