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,36 @@
from allauth.socialaccount.providers.base import (
ProviderAccount,
ProviderException,
)
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class WeiboAccount(ProviderAccount):
def get_profile_url(self):
# profile_url = "u/3195025850"
return "http://www.weibo.com/" + self.account.extra_data.get("profile_url")
def get_avatar_url(self):
return self.account.extra_data.get("avatar_large")
def to_str(self):
dflt = super(WeiboAccount, self).to_str()
return self.account.extra_data.get("name", dflt)
class WeiboProvider(OAuth2Provider):
id = "weibo"
name = "Weibo"
account_class = WeiboAccount
def extract_uid(self, data):
ret = data.get("idstr")
if not ret:
raise ProviderException("Missing 'idstr'")
return ret
def extract_common_fields(self, data):
return dict(username=data.get("screen_name"), name=data.get("name"))
provider_classes = [WeiboProvider]

View File

@@ -0,0 +1,30 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import WeiboProvider
class WeiboTests(OAuth2TestsMixin, TestCase):
provider_id = WeiboProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""
{"bi_followers_count": 0,
"domain": "", "avatar_large": "http://tp3.sinaimg.cn/3195025850/180/0/0",
"block_word": 0, "star": 0, "id": 3195025850, "city": "1", "verified": false,
"follow_me": false, "verified_reason": "", "followers_count": 6,
"location": "\u5317\u4eac \u4e1c\u57ce\u533a", "mbtype": 0,
"profile_url": "u/3195025850", "province": "11", "statuses_count": 0,
"description": "", "friends_count": 0, "online_status": 0, "mbrank": 0,
"idstr": "3195025850",
"profile_image_url": "http://tp3.sinaimg.cn/3195025850/50/0/0",
"allow_all_act_msg": false, "allow_all_comment": true, "geo_enabled": true,
"name": "pennersr", "lang": "zh-cn", "weihao": "", "remark": "",
"favourites_count": 0, "screen_name": "pennersr", "url": "", "gender": "f",
"created_at": "Tue Feb 19 19:43:39 +0800 2013", "verified_type": -1,
"following": false}
""",
)

View File

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

View File

@@ -0,0 +1,28 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import WeiboProvider
class WeiboOAuth2Adapter(OAuth2Adapter):
provider_id = WeiboProvider.id
access_token_url = "https://api.weibo.com/oauth2/access_token"
authorize_url = "https://api.weibo.com/oauth2/authorize"
profile_url = "https://api.weibo.com/2/users/show.json"
def complete_login(self, request, app, token, **kwargs):
uid = kwargs.get("response", {}).get("uid")
resp = requests.get(
self.profile_url, params={"access_token": token.token, "uid": uid}
)
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(WeiboOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(WeiboOAuth2Adapter)