Skip to content

Commit d8f7100

Browse files
authored
Add eBay compliance fix (#456)
* Add eBay compliance fix. * Fix formatting * Always provide string, not bytes. * token_type isn't always present.
1 parent 46f886c commit d8f7100

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Patches and Suggestions
2222
- Vinay Raikar <[email protected]>
2323
- kracekumar <[email protected]>
2424
- David Baumgold <[email protected]>
25+
- Craig Anderson <[email protected]>

requests_oauthlib/compliance_fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
from .mailchimp import mailchimp_compliance_fix
88
from .weibo import weibo_compliance_fix
99
from .plentymarkets import plentymarkets_compliance_fix
10+
from .ebay import ebay_compliance_fix
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
from oauthlib.common import to_unicode
3+
4+
5+
def ebay_compliance_fix(session):
6+
def _compliance_fix(response):
7+
token = json.loads(response.text)
8+
9+
# eBay responds with non-compliant token types.
10+
# https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html
11+
# https://developer.ebay.com/api-docs/static/oauth-auth-code-grant-request.html
12+
# Modify these to be "Bearer".
13+
if token.get("token_type") in ["Application Access Token", "User Access Token"]:
14+
token["token_type"] = "Bearer"
15+
fixed_token = json.dumps(token)
16+
response._content = to_unicode(fixed_token).encode("utf-8")
17+
18+
return response
19+
20+
session.register_compliance_hook("access_token_response", _compliance_fix)
21+
session.register_compliance_hook("refresh_token_response", _compliance_fix)
22+
23+
return session

requests_oauthlib/oauth2_session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ def fetch_token(
230230
`auth` tuple. If the value is `None`, it will be
231231
omitted from the request, however if the value is
232232
an empty string, an empty string will be sent.
233-
:param cert: Client certificate to send for OAuth 2.0 Mutual-TLS Client
234-
Authentication (draft-ietf-oauth-mtls). Can either be the
235-
path of a file containing the private key and certificate or
233+
:param cert: Client certificate to send for OAuth 2.0 Mutual-TLS Client
234+
Authentication (draft-ietf-oauth-mtls). Can either be the
235+
path of a file containing the private key and certificate or
236236
a tuple of two filenames for certificate and key.
237237
:param kwargs: Extra parameters to include in the token request.
238238
:return: A token dict

tests/test_compliance_fixes.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from requests_oauthlib.compliance_fixes import slack_compliance_fix
2020
from requests_oauthlib.compliance_fixes import instagram_compliance_fix
2121
from requests_oauthlib.compliance_fixes import plentymarkets_compliance_fix
22+
from requests_oauthlib.compliance_fixes import ebay_compliance_fix
2223

2324

2425
class FacebookComplianceFixTest(TestCase):
@@ -305,3 +306,29 @@ def test_fetch_access_token(self):
305306
"refresh_token": "iG2kBGIjcXaRE4xmTVUnv7xwxX7XMcWCHqJmFaSX",
306307
},
307308
)
309+
310+
311+
class EbayComplianceFixTest(TestCase):
312+
def setUp(self):
313+
mocker = requests_mock.Mocker()
314+
mocker.post(
315+
"https://api.ebay.com/identity/v1/oauth2/token",
316+
json={
317+
"access_token": "this is the access token",
318+
"expires_in": 7200,
319+
"token_type": "Application Access Token",
320+
},
321+
headers={"Content-Type": "application/json"},
322+
)
323+
mocker.start()
324+
self.addCleanup(mocker.stop)
325+
326+
session = OAuth2Session()
327+
self.fixed_session = ebay_compliance_fix(session)
328+
329+
def test_fetch_access_token(self):
330+
token = self.fixed_session.fetch_token(
331+
"https://api.ebay.com/identity/v1/oauth2/token",
332+
authorization_response="https://i.b/?code=hello",
333+
)
334+
assert token["token_type"] == "Bearer"

0 commit comments

Comments
 (0)