问题描述
我尝试使用仅限应用程序的身份验证 API
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"Some App Name" forHTTPHeaderField:@"User-Agent"];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setHTTPBody:[@"grant_type=client_credentials" dataUsingEncoding:NSUTF8StringEncoding]];
有趣的是,authValue 没有为 HTTPHeaderField @"Authorization" 设置.因此,我的电话当然会失败.所有其他标题字段设置正确.
Interestingly the authValue is not getting set for HTTPHeaderField @"Authorization". Therefor my calls fail of course. All other header fields are set correctly.
我检查它
NSLog(@"Authorization, %@, %@", [request valueForHTTPHeaderField:@"Authorization"], authValue);
返回@"Authorization, (null), theCorrectStuff
为什么会这样?我应该可以立即设置它,还是我遗漏了什么?
Why is this happening? I should be able to set it right away, or am I missing something?
感谢您的时间和考虑,法比安
Thanks for your time and consideration,Fabian
推荐答案
我遇到了同样的问题.输出您的 authValue,它可能包含新行.对于执行 base64 后的仅应用程序身份验证,您需要从 authValue 中删除新行,否则将不会设置标头字段.
I had the same issue. Output your authValue, it probably contains new lines. For the application only authentication after you do base64 you need to remove new lines from authValue otherwise the header field won't be set.
或者只是将已经编写好的 Objective-C 库用于 Twitter REST API 1.1 (https://github.com/nst/STTwitter).
Or just use already written Objective-C library for Twitter REST API 1.1 (https://github.com/nst/STTwitter).
这篇关于NSURLRequest setValue: forHTTPHeaderField:@"Authorization";不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!