From 585ae5d3a40bf85ac00c2e59993cff4d4d664db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Voron?= Date: Thu, 22 Aug 2024 14:52:43 +0200 Subject: [PATCH] Fix Github refresh token error It looks like they send JSON now! --- httpx_oauth/clients/github.py | 7 ++----- tests/test_clients_github.py | 7 +++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/httpx_oauth/clients/github.py b/httpx_oauth/clients/github.py index 8e4a733..8dd3c82 100644 --- a/httpx_oauth/clients/github.py +++ b/httpx_oauth/clients/github.py @@ -1,4 +1,3 @@ -import urllib.parse from typing import Any, Dict, List, Optional, Tuple, TypedDict, cast import httpx @@ -97,12 +96,10 @@ async def refresh_token(self, refresh_token: str) -> OAuth2Token: client, request, auth, exc_class=RefreshTokenError ) - content_type = response.headers.get("content-type", "") + data = self.get_json(response, exc_class=RefreshTokenError) # GitHub sends errors with a 200 status code - # and a form-urlencoded content type 😕 - if content_type.startswith("application/x-www-form-urlencoded"): - data = urllib.parse.parse_qs(response.text) + if "error" in data: raise RefreshTokenError(cast(str, data["error"]), response) data = self.get_json(response, exc_class=RefreshTokenError) diff --git a/tests/test_clients_github.py b/tests/test_clients_github.py index 2876133..e164675 100644 --- a/tests/test_clients_github.py +++ b/tests/test_clients_github.py @@ -1,5 +1,5 @@ +import json import re -from urllib.parse import urlencode import pytest import respx @@ -75,13 +75,12 @@ async def test_refresh_token_200_error(self): "error_description": "The refresh token passed is incorrect or expired.", "error_uri": "https://docs.github.com", } - error_response_encoded = urlencode(error_response) respx.post(client.refresh_token_endpoint).mock( return_value=Response( 200, - headers={"content-type": "application/x-www-form-urlencoded"}, - content=error_response_encoded, + headers={"content-type": "application/json"}, + content=json.dumps(error_response), ) )