2016-05-06 6 views
0

私はmozillaブラウザでrestクライアントを使用して認証サービスを呼び出しています。 身分証明書をBodyに渡すと、"auth-token"が得られます。次に、このヘッダーをブラウザーの[ヘッダー]タブのヘッダーに設定します。ポストコールPythonでヘッダを解析する

私は変数として私のpythonスクリプトのブラウザで設定しているこのヘッダを解析する必要があります。さらに、私のスクリプトでこの値を取得した後、その妥当性のためにトークンを認証する必要があります。 しかし、私は私のスクリプトでトークンの値を取得することができません。私の認証機能が準備完了です。トークンを取得するだけです ヘッダーからこのトークン値を取得する方法はありますか?

コード:

def check_authentication(auth): 

    print "Auth" , auth 

    chek_auth_url = ("http://10.168.2.161/auth/v/%s" % (auth)) 

    auth = requests.get(chek_auth_url) 

    if auth.status_code == 200: 

    return True 

私は、この関数でPARAMTERとしてトークンを渡し、認証のためにメインでこの関数で呼び出す必要があります。

def crossdomain(origin=None, methods=None, headers=None, max_age=21600, attach_to_all=True, automatic_options=True): 
if methods is not None: 
    methods = ', '.join(sorted(x.upper() for x in methods)) 
if headers is not None and not isinstance(headers, basestring): 
    headers = ', '.join(x.upper() for x in headers) 
if not isinstance(origin, basestring): 
    origin = ', '.join(origin) 
if isinstance(max_age, timedelta): 
    max_age = max_age.total_seconds() 

def get_methods(): 
    if methods is not None: 
     return methods 

    options_resp = current_app.make_default_options_response() 
    return options_resp.headers['allow'] 

def decorator(f): 
    def wrapped_function(*args, **kwargs): 
     if automatic_options and request.method == 'OPTIONS': 
      resp = current_app.make_default_options_response() 
     else: 
      resp = make_response(f(*args, **kwargs)) 
     if not attach_to_all and request.method != 'OPTIONS': 
      return resp 

     h = resp.headers 
     h['Access-Control-Allow-Origin'] = origin 
     h['Access-Control-Allow-Methods'] = get_methods() 
     h['Access-Control-Max-Age'] = str(max_age) 
     if headers is not None: 
      h['Access-Control-Allow-Headers'] = headers 
      #h['Access-Control-Allow-Headers'] = "Content-Type" 
     return resp 

    f.provide_automatic_options = False 
    return update_wrapper(wrapped_function, f) 
return decorator 

@app.route('/test', methods=['POST', 'OPTIONS']) 
@crossdomain(origin='*', headers='Content-Type') 
def get_storage(): 

    *check_authentication is called here and token is passed as a parameter* 
    *if token is valid further task i hav to do* 



if __name__ == '__main__': 

    app.run(host='192.168.56.1', port=8080, threaded=True) 
+0

ここで、ブラウザからトークンを取得するためのスクリプトはありますか? –

+0

私は1つを書いています。私は休憩を取ってから、難しいと思ったことがありません。私はpythonの初心者です –

+0

誰かが私の周りの仕事を提案することができますか? –

答えて

0

セルフヘルプは最高の助け..です

最後に私は修正が見つかりました:

トークン値が変数tokenValueにフェッチされます。私は今、さらにコーディングを行うことができます。

tokenValue = request.headers.get("token") 

if tokenValue == None: 
    return "x-auth-token not passed in header, please pass the token." 
else: 
    print "Token passed is", tokenValue 
関連する問題