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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With