使用cURL设计身份验证

don*_*zee 6 api curl ruby-on-rails devise

如何使用Devise从终端使用cURL验证我的Ruby on Rails应用程序?

我尝试着:

curl --user email:password http://domain.bla/api/auth/sign_in
Run Code Online (Sandbox Code Playgroud)

并在回应:

您要查找的页面不存在.

dya*_*yaa 7

这对我有用:

curl -XPOST -v -H 'Content-Type: application/json' http://domain/api/v1/auth/sign_in -d '{"email": "email@domain.com", "password": "password" }
Run Code Online (Sandbox Code Playgroud)

因此,我得到了响应(如下所示,只有重要部分):

< access-token: lW1c60hYkRwAinzUqgLfsQ
< token-type: Bearer
< client: W_xCQuggzNOVeCnNZbjKFw
< expiry: 1426610121
< uid: email@domain.com
Run Code Online (Sandbox Code Playgroud)

然后,我可以使用先前从上述请求中获得的客户端和令牌来验证令牌,如下所示:

curl -XGET -v -H 'Content-Type: application/json' -H 'access-token: lW1c60hYkRwAinzUqgLfsQ' -H 'client: W_xCQuggzNOVeCnNZbjKFw' -H "uid: email@domain.com" http://domain/api/v1/auth/validate_token
Run Code Online (Sandbox Code Playgroud)

结果 :

{"success":true,"data":{"id":3,"provider":"email","uid":"email@domain.com","firstname":null,"lastname":null,"email":"email@domain.com"}}
Run Code Online (Sandbox Code Playgroud)


too*_*ser 6

我发现登录步骤需要包含在user字典中,即:

curl -X POST -v -H 'Content-Type: application/json' \ 
  http://somehost.com/users/sign_in -d \ 
  '{"user" : {"email": "some_user@nowhereville.org", "password": "OpenSecret" }}'
Run Code Online (Sandbox Code Playgroud)

这是Devise 3.5.4.