Debian Codegraph @

allauth/socialaccount/providers/hubspot/views.py

raw
1 """Views for Hubspot API."""
2
3 from __future__ import annotations
4
5 from django.http import HttpRequest
6
7 from allauth.socialaccount.adapter import get_adapter
8 from allauth.socialaccount.providers.oauth2.views import (
9 OAuth2Adapter,
10 OAuth2CallbackView,
11 OAuth2LoginView,
12 )
13
14
15 class HubspotOAuth2Adapter(OAuth2Adapter):
16 """OAuth2Adapter for Hubspot API v3."""
17
18 provider_id = "hubspot"
19
20 authorize_url = "https://app.hubspot.com/oauth/authorize"
21 access_token_url = "https://api.hubapi.com/oauth/v1/token" # nosec
22 profile_url = "https://api.hubapi.com/oauth/v1/access-tokens"
23
24 def complete_login(self, request: HttpRequest, app, token, **kwargs):
25 headers = {"Content-Type": "application/json"}
26 with get_adapter().get_requests_session() as sess:
27 response = sess.get(f"{self.profile_url}/{token.token}", headers=headers)
28 response.raise_for_status()
29 extra_data = response.json()
30 return self.get_provider().sociallogin_from_response(request, extra_data)
31
32
33 oauth2_login = OAuth2LoginView.adapter_view(HubspotOAuth2Adapter)
34 oauth2_callback = OAuth2CallbackView.adapter_view(HubspotOAuth2Adapter)