【发布时间】: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