| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.socialaccount.app_settings import QUERY_EMAIL |
| 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 TwitterAPI(OAuth): |
| 15 |
""" |
| 16 |
Verifying twitter credentials |
| 17 |
""" |
| 18 |
|
| 19 |
_base_url = "https://api.x.com/1.1/account/verify_credentials.json" |
| 20 |
url = f"{_base_url}?include_email=true" if QUERY_EMAIL else _base_url |
| 21 |
|
| 22 |
def get_user_info(self): |
| 23 |
user = self.query(self.url).json() |
| 24 |
return user |
| 25 |
|
| 26 |
|
| 27 |
class TwitterOAuthAdapter(OAuthAdapter): |
| 28 |
provider_id = "twitter" |
| 29 |
request_token_url = "https://api.x.com/oauth/request_token" # nosec |
| 30 |
access_token_url = "https://api.x.com/oauth/access_token" # nosec |
| 31 |
# Issue #42 -- this one authenticates over and over again... |
| 32 |
# authorize_url = 'https://api.twitter.com/oauth/authorize' |
| 33 |
authorize_url = "https://api.x.com/oauth/authenticate" |
| 34 |
|
| 35 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 36 |
client = TwitterAPI(request, app.client_id, app.secret, self.request_token_url) |
| 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(TwitterOAuthAdapter) |
| 42 |
oauth_callback = OAuthCallbackView.adapter_view(TwitterOAuthAdapter) |