Devise_token_auth - 无法 运行 控制器操作
Devise_token_auth - unable to run controller action
我目前正在构建一个 iOS 应用程序,它与 ruby-on-rails api 进行通信。 api的用户management(sign_in,sign_out,...)部分用devise_token_auth
gem.[=处理24=]
我也在使用 Alamofire
来 运行 HTTP
请求。
我目前正在测试两个 api:
POST /api/auth/sign_in(.:format) devise_token_auth/sessions#create
POST /api/posts(.:format) api/posts#create
第一个 api 用于登录,而第二个创建 post。仅供参考,在我的 posts_controller
中,我使用 before_action :authenticate_user!
来防止未经身份验证的用户创建 posts.
这是我想要做的:
- 从客户端登录(iOS 应用程序)--> sign_in api returns 访问令牌
- 使用访问令牌从客户端创建 post。
我使用此代码成功获取了访问令牌:
let parameters = [
"email":"omar@omar.com",
"password":"password"]
Alamofire.request(.POST, "http://localhost:3000/api/auth/sign_in", parameters: parameters)
.responseJSON { response in
// get my access token and store it.
}
当我尝试创建 post(第 2 部分)时,这是我所做的:
let headers = ["access-token":"<my-token-value>",
"token-type": "Bearer",
"uid":"omar@omar.com"]
let parameters = [
"title":"this is a title",
"body":"this is a body"]
Alamofire.request(.POST, "http://localhost:3000/api/posts", parameters: parameters, headers: headers)
.responseJSON { response in
print(response.response) // URL response
}
打印响应时,这是我得到的:
Optional(<NSHTTPURLResponse: 0x7fe422f2b670> { URL: http://localhost:3000/api/posts } { status code: 401, headers {
"Cache-Control" = "no-cache";
Connection = "Keep-Alive";
"Content-Length" = 37;
"Content-Type" = "application/json; charset=utf-8";
Date = "Thu, 03 Mar 2016 15:03:27 GMT";
Server = "WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)";
"Set-Cookie" = "request_method=POST; path=/";
Vary = Origin;
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Request-Id" = "82a88799-3fac-42e0-bd01-db5b7e24fcda";
"X-Runtime" = "0.045969";
"X-Xss-Protection" = "1; mode=block";
} })
此外,在 Rails 服务器日志上:
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 15ms (Views: 0.8ms | ActiveRecord: 4.4ms)
有人知道错误是从哪里来的吗?
我终于找到了解决办法。除了期待 access-token
、token-type
和 uid
,我的 API 还期待 headers 中的 client
id。此 client
ID 在 sign_in
请求响应中发送。
我目前正在构建一个 iOS 应用程序,它与 ruby-on-rails api 进行通信。 api的用户management(sign_in,sign_out,...)部分用devise_token_auth
gem.[=处理24=]
我也在使用 Alamofire
来 运行 HTTP
请求。
我目前正在测试两个 api:
POST /api/auth/sign_in(.:format) devise_token_auth/sessions#create
POST /api/posts(.:format) api/posts#create
第一个 api 用于登录,而第二个创建 post。仅供参考,在我的 posts_controller
中,我使用 before_action :authenticate_user!
来防止未经身份验证的用户创建 posts.
这是我想要做的:
- 从客户端登录(iOS 应用程序)--> sign_in api returns 访问令牌
- 使用访问令牌从客户端创建 post。
我使用此代码成功获取了访问令牌:
let parameters = [
"email":"omar@omar.com",
"password":"password"]
Alamofire.request(.POST, "http://localhost:3000/api/auth/sign_in", parameters: parameters)
.responseJSON { response in
// get my access token and store it.
}
当我尝试创建 post(第 2 部分)时,这是我所做的:
let headers = ["access-token":"<my-token-value>",
"token-type": "Bearer",
"uid":"omar@omar.com"]
let parameters = [
"title":"this is a title",
"body":"this is a body"]
Alamofire.request(.POST, "http://localhost:3000/api/posts", parameters: parameters, headers: headers)
.responseJSON { response in
print(response.response) // URL response
}
打印响应时,这是我得到的:
Optional(<NSHTTPURLResponse: 0x7fe422f2b670> { URL: http://localhost:3000/api/posts } { status code: 401, headers {
"Cache-Control" = "no-cache";
Connection = "Keep-Alive";
"Content-Length" = 37;
"Content-Type" = "application/json; charset=utf-8";
Date = "Thu, 03 Mar 2016 15:03:27 GMT";
Server = "WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)";
"Set-Cookie" = "request_method=POST; path=/";
Vary = Origin;
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Request-Id" = "82a88799-3fac-42e0-bd01-db5b7e24fcda";
"X-Runtime" = "0.045969";
"X-Xss-Protection" = "1; mode=block";
} })
此外,在 Rails 服务器日志上:
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 15ms (Views: 0.8ms | ActiveRecord: 4.4ms)
有人知道错误是从哪里来的吗?
我终于找到了解决办法。除了期待 access-token
、token-type
和 uid
,我的 API 还期待 headers 中的 client
id。此 client
ID 在 sign_in
请求响应中发送。