2016-11-06 79 views
0

試圖執行上的Django 1.10的OAuth2認證+ Python 3中,我得到這個錯誤:rauth:JSON對象必須str的,而不是 '字節'

rauth: the JSON object must be str, not 'bytes'

它發生就行了電話: session = sso.get_auth_session(data=data, decoder=json.loads)

如果我刪除decoder=json.loads,我收到以下錯誤:

Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed.

這裏是我的方法:

def login(request): 
    """ 
    Login view 
    @author: Leonardo Pessoa 
    @since: 11/06/2016 
    """ 
    from rauth import OAuth2Service 
    from django.conf import settings 
    import json 

    # instantiating our login service with environment defined settings 
    sso = OAuth2Service(
     name    = settings.LOGIN_SETTINGS['name'], 
     client_id   = settings.LOGIN_SETTINGS['client_id'], 
     client_secret  = settings.LOGIN_SETTINGS['client_secret'], 
     access_token_url = settings.LOGIN_SETTINGS['access_token_url'], 
     authorize_url  = settings.LOGIN_SETTINGS['authorize_url'], 
     base_url   = settings.LOGIN_SETTINGS['base_url'], 
    ) 

    # check if we have a login code returned by OAuth 
    get_code = request.GET.get('code', '') 
    if get_code == '': 
     params = { 
      'redirect_uri' : settings.LOGIN_SETTINGS['redirect'], 
      'response_type' : 'code' 
     } 
     url = sso.get_authorize_url(**params) 

     return redirect(url) 
    else: 
     # we are in! 
     data = {'code'   : get_code, 
       'grant_type' : 'authorization_code', 
       'redirect_uri' : settings.LOGIN_SETTINGS['redirect'] 
       } 

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

     return redirect('/logado/') 

視圖是首次訪問的重定向到授權URL(無GET['code'])。接下來,再次訪問它(使用GET['code'])來處理授權碼並進行身份驗證。

任何想法?

回答

1

我仍然認爲應該有一些更直接的或錯誤修復到這一點,但這裏是我自己的解決方法:

def oauth_decode(data): 
    import json 

    new_data = data.decode("utf-8", "strict") 

    return json.loads(new_data) 
... 
session = sso.get_auth_session(data=data, decoder=oauth_decode) 

的問題是,get_auth_session字節流從響應到UTF-8解碼默認。如果你指定一個新的解碼器,你可以覆蓋它。所以我想我們需要在這裏(UTF-8 + JSON)。

相關問題