| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
from django.utils.http import urlencode |
| 5 |
|
| 6 |
from allauth.socialaccount.providers.oauth.client import OAuth |
| 7 |
from allauth.socialaccount.providers.oauth.views import ( |
| 8 |
OAuthAdapter, |
| 9 |
OAuthCallbackView, |
| 10 |
OAuthLoginView, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class FlickrAPI(OAuth): |
| 15 |
api_url = "https://api.flickr.com/services/rest" |
| 16 |
|
| 17 |
def get_user_info(self): |
| 18 |
default_params = {"nojsoncallback": "1", "format": "json"} |
| 19 |
p = dict({"method": "flickr.test.login"}, **default_params) |
| 20 |
u = self.query(f"{self.api_url}?{urlencode(p)}").json() |
| 21 |
|
| 22 |
p = dict( |
| 23 |
{"method": "flickr.people.getInfo", "user_id": u["user"]["id"]}, |
| 24 |
**default_params, |
| 25 |
) |
| 26 |
user = self.query(f"{self.api_url}?{urlencode(p)}").json() |
| 27 |
return user |
| 28 |
|
| 29 |
|
| 30 |
class FlickrOAuthAdapter(OAuthAdapter): |
| 31 |
provider_id = "flickr" |
| 32 |
request_token_url = "https://www.flickr.com/services/oauth/request_token" # nosec |
| 33 |
access_token_url = "https://www.flickr.com/services/oauth/access_token" # nosec |
| 34 |
authorize_url = "https://www.flickr.com/services/oauth/authorize" |
| 35 |
|
| 36 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 37 |
client = FlickrAPI(request, app.client_id, app.secret, self.request_token_url) |
| 38 |
extra_data = client.get_user_info() |
| 39 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 40 |
|
| 41 |
|
| 42 |
oauth_login = OAuthLoginView.adapter_view(FlickrOAuthAdapter) |
| 43 |
oauth_callback = OAuthCallbackView.adapter_view(FlickrOAuthAdapter) |