requests / requests-oauthlib

OAuthlib support for Python-Requests!
https://requests-oauthlib.readthedocs.org/
ISC License
1.71k stars 422 forks source link

ValueError: Unsupported token type: User Access Token #403

Closed thiras closed 4 years ago

thiras commented 4 years ago

Hello. I've got some error after successfully obtain a token from eBay.

As you can see at its documentation eBay returns "token_type": "User Access Token"

After getting the token without having a problem, when I try to request anything from its endpoint I get;

Traceback (most recent call last):
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/bin/rods", line 11, in <module>
    load_entry_point('rpi-order-data-sync', 'console_scripts', 'rods')()
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/thiras/HDD/freelancer/contentassasin/rpi-order-data-sync/rpi_order_data_sync/main.py", line 132, in sync_ebay_orders
    orders = ebay.get(
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/requests/sessions.py", line 543, in get
    return self.request('GET', url, **kwargs)
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py", line 477, in request
    url, headers, data = self._client.add_token(
  File "/home/thiras/.local/share/virtualenvs/rpi-order-data-sync-tA0i1rrc/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 192, in add_token
    raise ValueError("Unsupported token type: %s" % self.token_type)
ValueError: Unsupported token type: User Access Token 
thiras commented 4 years ago

I've changed token type with "Bearer" manually and it worked.

sambragg commented 4 years ago

I'm having this same issue with another provider. @thiras, if you don't mind - please could you tell me how you changed the token_type manually?

thiras commented 4 years ago

@sambragg sure. Check my solution here; https://stackoverflow.com/a/61001852/4132845

Also check register_compliance_hook at the official docs.

sambragg commented 4 years ago

This is great, thank you so much @thiras

sambragg commented 4 years ago

@thiras I couldn't get your solution to work. Incase anyone else has this issue, what I ended up doing was the following:

import json
from oauthlib.common import to_unicode

self.oauth.register_compliance_hook('access_token_response', self.non_compliant_token)
self.oauth.register_compliance_hook('refresh_token_response', self.non_compliant_token)

def non_compliant_token(self, response):
        token = json.loads(response.text)
        token["token_type"] = "Bearer"
        fixed_token = json.dumps(token)
        response._content = to_unicode(fixed_token).encode("utf-8")
        return response

This avoids having to create a fake class etc.