Debian Codegraph @

allauth/socialaccount/providers/notion/client.py

raw
1 from __future__ import annotations
2
3 from http import HTTPStatus
4 from requests.auth import HTTPBasicAuth
5 from urllib.parse import parse_qsl
6
7 from django.utils.http import urlencode
8
9 from allauth.socialaccount.adapter import get_adapter
10 from allauth.socialaccount.providers.oauth2.client import OAuth2Client, OAuth2Error
11
12
13 class NotionOAuth2Client(OAuth2Client):
14 def get_redirect_url(self, authorization_url, scope, extra_params) -> str:
15 scope = self.scope_delimiter.join(set(scope))
16 params = {
17 "client_id": self.consumer_key,
18 "scope": scope,
19 "response_type": "code",
20 "owner": "user",
21 }
22 if self.state:
23 params["state"] = self.state
24 return f"{authorization_url}?{urlencode(params)}"
25
26 def get_access_token(self, code, pkce_code_verifier=None):
27 with get_adapter().get_requests_session() as sess:
28 resp = sess.request(
29 self.access_token_method,
30 self.access_token_url,
31 auth=HTTPBasicAuth(self.consumer_key, self.consumer_secret),
32 json={"code": code, "grant_type": "authorization_code"},
33 headers=self.headers,
34 )
35 access_token = None
36 if resp.status_code in [HTTPStatus.OK, HTTPStatus.CREATED]:
37 try:
38 access_token = resp.json()
39 except ValueError:
40 access_token = dict(parse_qsl(resp.text))
41 if not access_token or "access_token" not in access_token:
42 raise OAuth2Error(f"Error retrieving access token: {resp.content}")
43 return access_token