Exchange an authorization code for tokens.
(
token_endpoint: str,
client_id: str,
code: str,
verifier: str,
redirect_uri: str,
)
| 300 | |
| 301 | |
| 302 | def _exchange_code( |
| 303 | token_endpoint: str, |
| 304 | client_id: str, |
| 305 | code: str, |
| 306 | verifier: str, |
| 307 | redirect_uri: str, |
| 308 | ) -> dict[str, Any]: |
| 309 | """Exchange an authorization code for tokens.""" |
| 310 | payload = urlencode( |
| 311 | { |
| 312 | "grant_type": "authorization_code", |
| 313 | "client_id": client_id, |
| 314 | "code": code, |
| 315 | "redirect_uri": redirect_uri, |
| 316 | "code_verifier": verifier, |
| 317 | } |
| 318 | ).encode() |
| 319 | |
| 320 | req = urllib.request.Request( |
| 321 | token_endpoint, |
| 322 | data=payload, |
| 323 | headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}, |
| 324 | ) |
| 325 | with urllib.request.urlopen(req, timeout=30) as resp: |
| 326 | return json.loads(resp.read()) |
| 327 | |
| 328 | |
| 329 | def _refresh_token(token_endpoint: str, client_id: str, refresh_token: str) -> dict[str, Any]: |