当我尝试连接Box.com的API时,rauth2 Decoder无法处理access_token

时间:2014-01-18 11:17:36

标签: python rauth

这是我连接Box的代码,但我无法使用box_storage.get_auth_session(data = data)。         来自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)

这是我得到的错误:

    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"}'

当我调用get_auth_session时,我很难弄清楚为什么我会收到此错误。它能是什么?

1 个答案:

答案 0 :(得分:12)

我明白了。我所做的只是将解码器更改为json.loads。例如,

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

修改的 对于Python 3,必须首先解码字节响应。你可以通过传递一个自定义解码器来做到这一点:

import json

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

session = box_storage.get_auth_session(data=data, decoder=new_decoder)