[英]how to properly authenticate user with devise_token_auth rails gem?
[英]How to successfully use validate_token in the devise_token_auth gem?
我對Ruby和Rails本身很新,我正在嘗試構建一個簡單的Rails API。
我想通過令牌驗證用戶,因此我使用devise_token_auth gem。
我可以在/ auth / sign_in成功發出POST請求,我現在正在嘗試在/ auth / validate_token發出GET請求
我所擁有的,作為“測試”:
uri = URI.parse("http://localhost:3000/auth/sign_in")
response = Net::HTTP.post_form(uri, {"email" => params[:session][:email], "password" => params[:session][:password]})
uri2 = URI.parse("http://localhost:3000/auth/validate_token")
params = { :auth_token => response['access-token'], :uid => response['uid'], :client => response['client'] }
uri2.query = URI.encode_www_form(params)
response2 = Net::HTTP.get_response(uri2)
因此,我使用從響應頭中檢索的訪問令牌和uid,但我不斷從/ auth / validate_token獲得401響應代碼:
Started GET "/auth/validate_token?auth_token=EEV40VDHfOaWtBzv3bn_DQ&uid=username%40example.com&client=NAzWNJalYBJLRni9dCGxXA" for ::1 at 2016-06-22 15:22:35 +0100
Processing by DeviseTokenAuth::TokenValidationsController#validate_token as */*
Parameters: {"auth_token"=>"EEV40VDHfOaWtBzv3bn_DQ", "uid"=>"username@example.com", "client"=>"NAzWNJalYBJLRni9dCGxXA"}
Completed 401 Unauthorized in 0ms (Views: 0.1ms | ActiveRecord: 0.0ms)
我究竟做錯了什么? 我怎么解決這個問題?
我認為這個問題是雙重的:
access-token
而不是auth_token
你可以在這個github問題中閱讀它。 它可能不是您出現問題的時候,但它目前已在README中發布。
以下是有效身份驗證請求所需的所有標頭(在撰寫本文時):
"access-token": "wwwww", "token-type": "Bearer", "client": "xxxxx", "expiry": "yyyyy", "uid": "zzzzz"
注意:這些對於每個端點都不是必需的,但通常是access-token
, client
和uid
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.