2016-03-29 11 views
0

私の角クライアントから$ http getリクエストを私のDjangoバックエンドに送ります。ここで(request.user)は私にを与えます。匿名。このため、私はサーバー側で何かできます。django angonymous匿名ユーザー

私は、認証を行うために、クライアント側でジャンゴ側のジャンゴ - 残り-AUTH角度-ジャンゴ登録-AUTHを使用しています。認証自体は成功です。私はログインし、サーバーから送信されたクライアント側のユーザー名、電子メールIDを取得できます。

私の要求は、次のようになります。

私のアプリの設定で
var url = "http://localhost:8000/api/exercises/get_exercise/" + exerciseType + "/"; 

    $http({ 
      url: url, 
      method: 'GET', 
      headers: {'X-CSRFToken': $cookies['csrftoken']} 
     }) 

      .success(function(response){ 

      console.log(response); 

      }); 
     }) 

私は、次のしている行を追加し、次のように

私のChromeブラウザのコンソールから見た
$httpProvider.defaults.withCredentials = this.use_session; 

リクエストヘッダは次のとおりです。

Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,en-GB;q=0.2 
Authorization:Token eb979cb6f179dd2d9056023685e2d02e4e65a58e 
Connection:keep-alive 
Host:localhost:8000 
Origin:http://localhost:8100 
Referer:http://localhost:8100/ 
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 

「」の私のdjango apiエンドポイントから同じURLを直接押した場合「私のジャンゴ・サーバが実行されている、それは成功し、次のようにリクエストヘッダは以下のとおりです。

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,en-GB;q=0.2 
Cache-Control:max-age=0 
Connection:keep-alive 
Cookie:csrftoken=GDco30gJ9vki6LDtqJSuQh9hGB0aXp84;  sessionid=94xfwx9zvr4pgd1wx9r0nemjwmy3mowi 
Host:127.0.0.1:8000 
Upgrade-Insecure-Requests:1 
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 

私が見るの違いは、成功した要求は、それが送信された要求から欠落しているとして、クッキーやセッション情報を、送信していることです私の角張ったアプリから。

理由と回避策は何ですか?

私のDjangoのsettings.pyには、以下のい:

REST_FRAMEWORK = { 
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.BasicAuthentication', 
    'rest_framework.authentication.SessionAuthentication', 
    'rest_framework.authentication.TokenAuthentication', 
), 
'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated', 
), 

} 

私のDjangoのビューには、次のようになります。

from django.contrib.auth.models import User 
from rest_framework.authentication import TokenAuthentication,  SessionAuthentication, BasicAuthentication 
from rest_framework.permissions import IsAuthenticated 
from rest_framework.response import Response 
from rest_framework.views import APIView 
from rest_framework.decorators import api_view 
from rest_framework.decorators import authentication_classes 
from rest_framework.decorators import permission_classes 

class JSONResponse(HttpResponse): 
authentication_classes = (SessionAuthentication, BasicAuthentication) 
permission_classes = (IsAuthenticated,) 
""" 
An HttpResponse that renders its content into JSON. 
""" 
def __init__(self, data, **kwargs): 
    content = JSONRenderer().render(data) 
    kwargs['content_type'] = 'application/json' 
    super(JSONResponse, self).__init__(content, **kwargs) 

#view to get a specific exercise for a particular user 
def get_single_exercise_for_user_id(request, exerciseId): 

    results = Exercise_state_ui_model.fetch_exercises(request.user.id, exerciseId) 

    serializer = ExerciseStateSerializer(results, many=True) 
    return JSONResponse(serializer.data) 

答えて

1
Authorization:Token eb979cb6f179dd2d9056023685e2d02e4e65a58e 

は、私はあなたがそのビューにトークン認証を有効にする必要があると思うします。

#view to get a specific exercise for a particular user 
@api_view(('GET',)) 
def get_single_exercise_for_user_id(request, exerciseId): 

    results = Exercise_state_ui_model.fetch_exercises(request.user.id, exerciseId) 

    serializer = ExerciseStateSerializer(results, many=True) 
    return Response(serializer.data) 
+0

settings.pyとviews.pyのスニペットで質問を更新しました。行方不明のものがあるかどうか調べることができますか? – Nitish

+0

そのビューを飾る必要があります。また、JSONレスポンスを取り出し、クライアントが要求する内容に基づいてrest_frameworkが返すコンテンツを決定させます。 – kevswanberg

+0

驚くばかり!あなたのソリューションはうまくいった!私は2日間、すべての可能な理由を探して無駄にしました!どうもありがとう! – Nitish

関連する問題