I am currently building an iOS app which communicates with a ruby-on-rails api. The user management(sign_in, sign_out,...) part of the api is handled with devise_token_auth
gem.
我目前正在构建一个与ruby-on-rails api通信的iOS应用程序。 api的用户管理(sign_in,sign_out,...)部分由devise_token_auth gem处理。
I am also using Alamofire
to run HTTP
requests.
我也在使用Alamofire来运行HTTP请求。
I am currently testing two apis:
我目前正在测试两个api:
POST /api/auth/sign_in(.:format) devise_token_auth/sessions#create
POST /api/posts(.:format) api/posts#create
POST /api/auth/sign_in(.:format)devise_token_auth / sessions #create
POST /api/posts(.:format)api / posts #create
The first api is used to login while the second creates posts. FYI, in my posts_controller
, I am using before_action :authenticate_user!
to prevent non authenticated users to create posts.
第一个api用于登录,而第二个api用于创建帖子。仅供我在posts_controller中使用before_action:authenticate_user!防止未经过身份验证的用户创建帖子。
Here is what I want to do:
这是我想要做的:
- Sign in from the client side (iOS app) --> the sign_in api returns the access-token
- Create a post from the client side using the access-token.
从客户端登录(iOS应用程序) - > sign_in api返回访问令牌
使用访问令牌从客户端创建帖子。
I managed to get the access-token using this code:
我设法使用以下代码获取访问令牌:
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.
}
When I try to create a post(2nd part), here is what I did :
当我尝试创建一个帖子(第二部分)时,我就是这样做的:
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
}
When printing the response, here is what I get:
打印响应时,我得到的是:
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";
} })
Also, on Rails server logs:
另外,在Rails服务器日志上:
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 15ms (Views: 0.8ms | ActiveRecord: 4.4ms)
在15ms完成401未授权(浏览次数:0.8ms | ActiveRecord:4.4ms)
Does anyone know where the error is coming from?
有谁知道错误的来源?
1 个解决方案
#1
1
I finally found the solution. Beside expecting the access-token
, the token-type
and the uid
, my API was also expecting the client
id in the headers. This client
id is sent at the sign_in
request response.
我终于找到了解决方案。除了期望访问令牌,令牌类型和uid之外,我的API还期望标头中的客户端ID。此客户端ID在sign_in请求响应中发送。
#1
1
I finally found the solution. Beside expecting the access-token
, the token-type
and the uid
, my API was also expecting the client
id in the headers. This client
id is sent at the sign_in
request response.
我终于找到了解决方案。除了期望访问令牌,令牌类型和uid之外,我的API还期望标头中的客户端ID。此客户端ID在sign_in请求响应中发送。