2017-07-27 5 views
0

oauth_2認証が成功したことを確認するためにdjangoセッションデータを使用しています。しかし、djangoはビュー間でセッションデータを保存していません。Djangoは他のビューへのリダイレクト間セッションを保存していません

@never_cache 
def login(request): 

    microsoft = OAuth2Session(client_id,scope=scope,redirect_uri=redirect_uri) 
    global state 
    authorization_url, state = microsoft.authorization_url(authorization_base_url) 
    # State is used to prevent CSRF, keep this for later. 
    request.session['oauth_state'] = state 

    return HttpResponseRedirect(authorization_url) 
@never_cache 
def authorization(request): 
    print(request.session.get('oauth_state')) ##This is where I'm having a problem. 'oauth_state' prints none! 

    microsoft = OAuth2Session(client_id,scope=scope,redirect_uri=redirect_uri) 
    token = "" 
    try: 
     users = 'https://graph.microsoft.com/v1.0/me' ##msgraph query url- 
                  ##This query is purelyjust used to 
                  ##authenticate user! 
     token = microsoft.fetch_token(token_url, client_secret=client_secret,code=request.GET.get('code', '')) 
     header = {'Authorization': 'Bearer ' + token['access_token']} 
     response = requests.get(url = users, headers = header) 
     print(response.text) 
     print(response.status_code) 
     if int(response.status_code) != 200: ##if status code is not 200, then authentication failed. Redirect to login. 
      print ('Not validated. Return to login.') 
      return redirect('http://localhost:8000/login') 
     check_for_authorized = True 
     print(token) 
    except Exception as e: 
     print ('User not does not have authentication rights') 
     return redirect('http://localhost:8000/login') 

    return HttpResponseRedirect('http://localhost:8000/search') 

認可の最初の行の下にある自分の印刷状態の横のコメントを見てください。なぜあなたはこれがだと思いますか?ビュー間でセッションデータを共有しないでください。

+0

を保存した後、これを使用してください。あなたは、国家のためのグローバルの使用法を説明できますか?実際に何かのためにそれを必要としない場合は、それを削除してみてください。 –

+0

'state'変数にはglobalを絶対に使用しないでください。 –

答えて

関連する問題