2016-05-23 5 views
0

現在、私は小さなアプリケーションを作成しており、問題が発生しています。私はユーザーがphpで公開されているapiにアクセスできるようにトークンを取得するためにフラスコのjwtモジュールを使用しています。私はフロントエンドの開発にangularjsを使用しています。私は、APIのドキュメンテーションに応じて400エラーにリターンにFlask jwt auth endpointが400を返します

"POST /auth HTTP/1.1" 400 

を常駐サーバにログインしようとすると、私がしなければならないすべては、認証エンドポイントに私の資格情報を渡すと、私は戻って取得することができるはずですページ上で実証されたようにトークン:https://pythonhosted.org/Flask-JWT/

ここでは私の現在のアプリケーション・サーバの実装です:

from flask import Flask 
from flask_jwt import JWT, jwt_required, current_identity 
from flask.ext.cors import CORS 
from werkzeug.security import safe_str_cmp 
from wol import User, db 
import hashlib 


def authenticate(username, password): 
    user = User.query.filter_by(username=username).first() 
    print str(user) 
    if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')): 
     return user 


def identity(payload): 
    user_id = payload['identity'] 
    return User.query.filter_by(UserID=user_id) 


app = Flask(__name__) 
CORS(app) 
app.debug = True 
app.config['SECRET_KEY'] = 'super-secret' 

jwt = JWT(app, authenticate, identity) 
admin = User("test", "test1") 
db.session.add(admin) 
db.session.commit() 

@app.route('/protected') 
@jwt_required() 
def protected(): 
    return '%s' % current_identity 


@app.route('/register', methods=['POST']) 
def register(username, password, confirmPassword): 
    # we probably want to hash these passwords when storing in db 
    # we'll hash both the password and confirm password 
    pass_hash = hashlib.sha256(password).hexdigest() 
    conf_pass_hash = hashlib.sha256(confirmPassword).hexdigest() 

    if pass_hash == conf_pass_hash: 
     new_user = User(username, password) 
     db.session.add(new_user) 
     db.session.commit() 

@app.route('/allusers') 
def get_all_users(): 
    users = User.query.all() 
    return users 


if __name__ == '__main__': 
    app.run(host='0.0.0.0') 

、ここでは私のangularjs実装です:

(function() { 
     angular.module('loginApp', []).controller('loginController', function($scope, $http) { 
     $scope.data = {}; 

     //we can now process the form 
     $scope.processForm = function() { 
      $http({ 
      method : 'POST', 
      url  : 'http://192.168.0.99:5000/auth', 
      data : $.param($scope.data), // pass in data as strings 
      headers : { 'Content-Type': 'application/json' } // set the headers so angular passing info as form data (not request payload) 
     }) 
      .success(function(data) { 
      console.log("successful") 
      console.log(data); 
      }); 
     }; 
     }); 
    }()); 

が、私は私のPythonコードを実行するサーバー上でデバッグモードを設定しているし、それが400

+0

400のレスポンスボディとは何ですか? – univerio

答えて

1

ああを返し、クエリ文字列パラメータにオブジェクトをシリアライズ$.paramを、使用しているので、これはですが、APIはJSONを期待します。次のようにする必要があります。

data: JSON.stringify($scope.data) 

関連する問題