【问题标题】:NSURLRequest setValue: forHTTPHeaderField:@"Authorization" not workingNSURLRequest setValue: forHTTPHeaderField:@"Authorization" 不起作用
【发布时间】:2013-08-31 12:42:23
【问题描述】:

我尝试使用 Application only Authentication API

对 twitter 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" 设置。因此,我的电话当然会失败。所有其他标头字段均已正确设置。

我检查一下

NSLog(@"Authorization, %@, %@", [request valueForHTTPHeaderField:@"Authorization"], authValue);

返回@"Authorization, (null), theCorrectStuff

为什么会这样?我应该可以立即设置它,还是我错过了什么?

感谢您的时间和考虑, 费边

【问题讨论】:

    标签: objective-c twitter-oauth nsurlrequest http-authentication nsmutableurlrequest


    【解决方案1】:

    我有同样的问题。输出您的 authValue,它可能包含新行。对于执行 base64 后的仅应用程序身份验证,您需要从 authValue 中删除新行,否则将不会设置标头字段。

    或者只是使用已经为 Twitter REST API 1.1 (https://github.com/nst/STTwitter) 编写的 Objective-C 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      相关资源
      最近更新 更多