【问题标题】:oAuth2 authentication of server API using NSURLSessions in ios在 ios 中使用 NSURLSessions 对服务器 API 进行 oAuth2 身份验证
【发布时间】:2015-01-14 08:01:07
【问题描述】:

如果您发现这个问题与完整的上下文相似或不清楚,请多多包涵,发布我的第一个问题,这样我会在习惯后改进。 我试图搜索类似的问题陈述以找到接近的解决方案。

我的 Web 服务器提供了客户端 ID、机密、重定向 URL,以便它可以使用 oauth2 身份验证对其 API 进行身份验证以供使用。因此,在使用其任何 Web 服务之前,一开始客户端必须进行授权令牌握手以接收要提供给 API 调用的令牌。

在我的 IOS 客户端应用程序中创建一个 NSURL,如下所示:

NSString* urlString = [NSString stringWithFormat:@"%@/oauth/authorize/?client_id=%@&response_type=code&redirect_uri=%@",myServerHostName, myAppClientId,myServerRedirectHostname];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"utf-8"  forHTTPHeaderField:@"charset"];
[request setHTTPMethod:@"POST"];

使用上面的 NSURLRequest 创建一个带有适当参数的 NSURLSession 和 NSURLSessionDataTask:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request];
[postDataTask resume];

并创建了一个委托重定向处理程序,因此我可以获取新的重定向 URL,例如:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionDataTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)newRequest completionHandler:(void (^)(NSURLRequest *))completionHandler {

      //Doing stuff : Grab the new redirected URL to get the oauth2 code. from the URL.
}

这可以是在访问/使用之前执行 oauth2 以验证 API 的合法方式吗?

目前我的服务器在收到上述请求时失败并抛出 500。

Completed 500 Internal Server Error in 14ms

NameError (undefined local variable or method `login_path' for #<Doorkeeper::AuthorizationsController:0x000001078c1560>)

但是当我通过网络尝试此操作时:使用“高级 REST 客户端”的示例,客户端成功接收到重定向 URL(状态代码为 302)。

【问题讨论】:

    标签: ios authentication oauth


    【解决方案1】:

    后来发现发送请求是绝对正确的,服务器端使用了门卫 gem 实现,实习生重定向到注册链接进行身份验证,因为重定向失败,因此收到错误 500。如果没有理想的用户帐户,任何 API 的此类 oauth2 身份验证都无法完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-24
      • 2020-06-24
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多