ios OAuth 2 不记名授权标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12091341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OAuth 2 bearer Authorization header
提问by rckoenes
With an update to the client's API the HTTPBasicAuthication method has been replace with a OAuth2 Bearer
Authorization header.
随着客户端 API 的更新,HTTPBasicAuthication 方法已替换为 OAuth2Bearer
授权标头。
With the old API I would do the following:
使用旧 API,我将执行以下操作:
NSURLCredential *credential = [NSURLCredential credentialWithUser:self.account.username
password:self.account.token
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:kAPIHost
port:443
protocol:NSURLProtectionSpaceHTTPS
realm:@"my-api"
authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
But this will not work with the Bearer
header.
但这不适用于Bearer
标题。
Now normally I would just add the header my self by adding it like so:
现在通常我会像这样添加标题我自己:
NSString *authorization = [NSString stringWithFormat:@"Bearer %@",self.account.token];
[urlRequest setValue:authorization forHTTPHeaderField:@"Authorization"];
But the problem with this solutions is that the API redirect most of the calls to other URLs, this has to do with security.
After the NSURLRequest
gets redirected the Authorization header is removed from the request and since I'm unable to add the Bearer method to the NSURLCredentialStorage
it can't authenticate any more after being redirected.
但是这个解决方案的问题是 API 将大部分调用重定向到其他 URL,这与安全性有关。之后,NSURLRequest
被重定向的授权头从请求中删除,因为我无法承载方法添加到NSURLCredentialStorage
它被重定向后无法验证了。
What would be a good solutions? I can only think to catch the redirect and modify the NSURLRequest
so it does include the Bearer
header. But how?
什么是好的解决方案?我只能想到捕获重定向并修改NSURLRequest
它,因此它确实包含Bearer
标题。但是如何?
回答by rckoenes
Well after much research I found out that I will just have to replace the NSURLRequest
when a call is redirected.
经过大量研究,我发现我只需要NSURLRequest
在呼叫重定向时替换。
Not as nice as I would like it to be, but is does work.
并不像我想它是,但是是很好的做工作。
I used AFNetworking
and added the redirect block, then check wether the Authorization
header is still set if not I create a new NSMutableURLRequest
and set all the properties to match the old request (I know I could have just created a mutable copy):
我使用AFNetworking
并添加了重定向块,然后检查Authorization
标头是否仍然设置,如果没有我创建一个新的NSMutableURLRequest
并设置所有属性以匹配旧请求(我知道我可以创建一个可变副本):
[requestOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
if ([request.allHTTPHeaderFields objectForKey:@"Authorization"] != nil) {
return request;
}
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:request.URL cachePolicy:request.cachePolicy timeoutInterval:request.timeoutInterval];
NSString *authValue = [NSString stringWithFormat:@"Bearer %@", self.account.token];
[urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
return urlRequest;
}];
回答by MadNik
I'm using AFNetworking Library
我正在使用 AFNetworking 库
Find AFHttpClient.mand you have a method
找到AFHttpClient.m,你就有了一个方法
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
}
replace this method with the following or if you need it for back compatibility keep it an add with a different name and use that name
将此方法替换为以下内容,或者如果您需要它以实现向后兼容性,请使用不同的名称添加并使用该名称
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Bearer %@", token]];
}
then make the request with oauth access token. (Following is a GET method service)
然后使用 oauth 访问令牌发出请求。(以下是GET方法服务)
NSURL *url = [EFServiceUrlProvider getServiceUrlForMethod:methodName];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithToken:@"add your access token here"];
[httpClient getPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
}];
Updated
更新
Use Oauth2 Client on AFNetworking written by matt
在 AFNetworking 上使用 Oauth2 Client by matt
回答by Andrespch
If you happen to be having this issue with Django rest framework and the routers the problem might be related to the trailing slash being clipped by the NSUrlRequest. if the trailing slash is clipped then django will have to redirect your request, to avoid this you can use Trailing_slash = True like this
如果您碰巧在 Django rest 框架和路由器上遇到此问题,则问题可能与 NSUrlRequest 剪切的尾部斜杠有关。如果尾部斜杠被剪裁,则 django 将不得不重定向您的请求,为避免这种情况,您可以像这样使用 Trailing_slash = True
router = routers.DefaultRouter(trailing_slash=False)
That way not your authorization header nor your parameters will get lost.
这样,您的授权标头和参数都不会丢失。
Hope this saves somebody some time.
希望这可以节省一些时间。