我如何使用NSURLSession发出OAuth请求?

时间:2015-10-14 05:06:05

标签: ios objective-c nsurlsession

我有一个客户端ID和秘密,我应该在post请求中发送,以便接收访问API(OAuth)的令牌。我正在使用:

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

设置我的授权标头;但是,我一直回来:

{
    error = unauthorized;
    "error_description" = "Full authentication is required to access this resource";
}

我有双重和三重检查我的客户ID和秘密,但我一直得到这个回复。

以下是我用来提出请求的代码:

    NSURL *baseURL = [NSURL URLWithString:@"http://"];
    NSURL *url = [NSURL URLWithString: @"/oauth/token?" relativeToURL:baseURL];
    NSURL *absoluteURL = [url absoluteURL];

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfiguration.HTTPAdditionalHeaders = @{@"Authorization" : [CLIENT_SECRET_PREFIX stringByAppendingString:[self base64Encode:CLIENT_SECRET_TOKEN]]};
    sessionConfiguration.HTTPAdditionalHeaders = @{@"Content-Type" : @"application/json"};

    NSURLSession *session =[NSURLSession sessionWithConfiguration:sessionConfiguration];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:absoluteURL];
    [[session dataTaskWithRequest:request
               completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                   NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                   NSLog(@"%@", json);
    }] resume];

任何人都可以帮我解决这个问题吗?在此先感谢。

0 个答案:

没有答案