如何通过授权令牌头目标C? [英] How to pass authorization token header objective c?

查看:419
本文介绍了如何通过授权令牌头目标C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我路过的用户名,密码API调用返回正确的价值。但经过认证API返回一个安全令牌,我需要捕捉令牌,以及用户名和password.I'm做同样的通过,但它返回它表示无效令牌error.I也试过的base64通过令牌一些的建议禁止错误答案stackoverflow.The code我用下面通过

I'm passing username,password for an API call it returns right value. But after authentication API returns an security token I need to capture the token and pass along with username and password.I'm doing the same but it returns forbidden error which denotes invalid token error.I also tried base64 to pass token as suggested at some answer in stackoverflow.The code I use to pass header values below

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:[self HTTPMethod]];
[request setHTTPBody:self.requestData];
[request addValue:@"xyz" forHTTPHeaderField:@"username"];
[request addValue:@"xyz" forHTTPHeaderField:@"password"];
[request addValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"Auth"] forHTTPHeaderField:@"Authorization"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

我搜索头的方法,但没有什么helps.what我做错了here.pls人帮助我解决it.Thanks。

I searched for header methods, But nothing helps.what I'm doing wrong here.pls anybody help me fix it.Thanks.

推荐答案

使用的setValue 的要求,而不是的addValue 因为的addValue 如果值是$ p $指定字段使用相应的字段分隔符,所提供的值附加到现有值pviously设置

Use setValue for request instead of addValue because with addValue If a value was previously set for the specified field, the supplied value is appended to the existing value using the appropriate field delimiter

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:[self HTTPMethod]];
[request setHTTPBody:self.requestData];
[request setValue:@"xyz" forHTTPHeaderField:@"username"];
[request setValue:@"xyz" forHTTPHeaderField:@"password"];
[request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"Auth"] forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

检查文档 - 苹果

check apple documentation-

<一个href=\"https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/#//apple_ref/occ/instm/NSMutableURLRequest/setValue:forHTTPHeaderField\" rel=\"nofollow\">https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/#//apple_ref/occ/instm/NSMutableURLRequest/setValue:forHTTPHeaderField

这篇关于如何通过授权令牌头目标C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆