Skip to content

Commit

Permalink
Fix Github refresh token error
Browse files Browse the repository at this point in the history
It looks like they send JSON now!
frankie567 committed Aug 22, 2024
1 parent 600aa59 commit 585ae5d
Showing 2 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions httpx_oauth/clients/github.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 3 additions & 4 deletions tests/test_clients_github.py
Original file line number Diff line number Diff line change
@@ -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),
)
)

0 comments on commit 585ae5d

Please sign in to comment.