【问题标题】:JSON API Request after Authentication认证后的 JSON API 请求
【发布时间】:2013-11-19 19:43:26
【问题描述】:

伙计们,

在 iOS 中,我想在身份验证后发送 API 链接的身份验证我想使用 Accept: application/json 处理另一个 JSON 请求 API

我该怎么做?

我尝试了这种方法,但它不起作用:

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL for authentication"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    //[request setHTTPBody:data];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


    [connection start];

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {
        NSLog(@"received authentication challenge");
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"username"
                                                                    password:@"pass"
                                                                 persistence:NSURLCredentialPersistenceForSession];
        NSLog(@"credential created");
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];



        NSLog(@"responded to authentication challenge");

        // Second JSON Request API here


    }
    else {
        NSLog(@"previous authentication failure");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"%@",response);

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {


}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

}

查看此链接

https://checkout.hackathon.sla-alacrity.com/catalogs/443b70de5cd4429e43e9da9b87d6468058d0ae737fd6b8771

这是表演

{ “错误”: { "message": "Accept header 无效。需要通过 HTTP Header Accept: application/json", “类型”:“bad_request” } }

【问题讨论】:

    标签: ios objective-c json authentication


    【解决方案1】:

    您可以查看此answer。我也建议使用AFNetworking

    如果您使用的是基本 HTTP 身份验证,我认为不需要专门请求身份验证 URL,您应该首先简单地请求 JSON API。如果您事先有用户名和密码,则应在发出请求之前将它们设置在标头中:

    #import "NSData+Base64.h"
    ...
    NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"username", @"pass"];
    NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    

    上面的代码有 NSData+Base64.h 依赖,你需要将它添加到你的项目中,你可以下载到:NSData+Base64 class and header

    如果您提示用户输入凭据(代码中未显示)并且您确实需要在连接中设置凭据:didReceiveAuthenticationChallenge 仍然适用,您可以只使用您尝试创建的 URL在身份验证质询方法中:

    // Second JSON Request API here
    

    【讨论】:

    • 我从我告诉你的帖子中复制了答案,它具有 AFNetworking 依赖项。我更新了也有依赖关系的代码,但我提供了下载链接
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2020-07-25
    • 2016-11-02
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多