Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rauth2 Decoder failed to handle access_token when I tried to connect with Box.com's API

Tags:

python

rauth

This is the code I have for connecting to Box, but I can't get box_storage.get_auth_session(data=data) to work. from rauth import OAuth2Service

    box_storage = OAuth2Service(
                                name='Box',
                                client_id=CLIENT_ID,
                                client_secret=CLIENT_SECRET,
                                authorize_url='https://www.box.com/api/oauth2/authorize',
                                access_token_url='https://www.box.com/api/oauth2/token',
                                base_url='https://www.box.com/'
                                )

    redirect_uri = 'http://127.0.0.1'

    params = {          
              'redirect_uri': redirect_uri,
              'response_type': 'code',
              'state': 'good'          
              }
    url = box_storage.get_authorize_url(**params)

    data = {'code': 'foobar',
            'grant_type': 'authorization_code',
            'redirect_uri': redirect_uri}
    session = box_storage.get_auth_session(data=data)

This is the error I get:

    Traceback (most recent call last):
      File "C:\Users\rushd\Documents\Aptana Studio 3 Workspace\practice\box.py", line 24, in <module>
        session = box_storage.get_auth_session(data=data)
      File "C:\Users\rushd\Envs\practice\lib\site-packages\rauth\service.py", line 533, in get_auth_session
        return self.get_session(self.get_access_token(method, **kwargs))
      File "C:\Users\rushd\Envs\practice\lib\site-packages\rauth\service.py", line 519, in get_access_token
        access_token, = process_token_request(r, decoder, key)
      File "C:\Users\rushd\Envs\practice\lib\site-packages\rauth\service.py", line 24, in process_token_request
        raise KeyError(PROCESS_TOKEN_ERROR.format(key=bad_key, raw=r.content))
    KeyError: 'Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed. Provider returned: {"error":"invalid_grant","error_description":"Auth code doesn\'t exist or is invalid for the client"}'

I'm having a hard time trying to figure out why I'm getting this error when I call get_auth_session. What can it be?

like image 576
rushd Avatar asked Jan 18 '14 11:01

rushd


1 Answers

I figured it out. All I did was change the decoder to json.loads. For example,

session = box_storage.get_auth_session(data=data, decoder=json.loads)

Edit For Python 3, the bytes response has to be decoded first. You can do that by passing it a custom decoder:

import json

def new_decoder(payload):
    return json.loads(payload.decode('utf-8'))

session = box_storage.get_auth_session(data=data, decoder=new_decoder)
like image 181
rushd Avatar answered Oct 22 '22 05:10

rushd