我试图通过在obj c中使用Oauth身份验证来获取数据,但是我发现错误“oauth_problem = signature_invalid”

时间:2016-08-17 14:17:35

标签: objective-c json parsing

-(void)getData:(void (^) (NSArray * result,NSError * error))completion{
    NSDictionary * jsonDic;
    __block NSString *responsebody = @"";
    OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"4ff70fabfa83728ca0febe33eaaa7c25"
                                                    secret:@"166980cbde6623913b66197458d658f2"];

    NSURL *url = [NSURL URLWithString:@"http://ordering.sonitekinternational.com/api/rest/customers/14/addresses"];
    OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
                                                                   consumer:consumer
                                                                      token:nil
                                                                      realm:nil
                                                          signatureProvider:nil];
    [request prepare];
    NSString *oauthHeader = @"OAuth ";
    oauthHeader = [oauthHeader stringByAppendingFormat:@"oauth_consumer_key=\"%@\"",@"4ff70fabfa83728ca0febe33eaaa7c25"];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_token=\"%@\"",@"ac42663555d64e1707b109b100240842"];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature_method=\"%@\"",@"HMAC-SHA1"];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature=\"%@\"",[self encodeToPercentEscapeString:request.signature]];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_timestamp=\"%ld\"", (time_t) [[NSDate date] timeIntervalSince1970]];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_nonce=\"%@\"",request.nonce];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_version=\"1.0\""];
    oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_token_secret=\"%@\"",@"04b9f08ffd1fe00b5bda0241c2705ecb"];
       [request setValue:oauthHeader forHTTPHeaderField:@"Authorization"];

    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"GET"];
    NSLog(@"Complete url is ==>>%@",request.URL);
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]
                                      dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data, NSURLResponse * _Nullable response, NSError *_Nullable error)
                                      {
                                          // Use the data here
                                          if(data == nil){
                                              completion(nil,error);
                                              return;
                                          }
                                          else{
                                              NSHTTPURLResponse *newResp = (NSHTTPURLResponse*)response;
                                              NSLog(@"%ld", (long)newResp.statusCode);
                                              NSMutableArray *parsedObject;
                                              parsedObject=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
                                              NSLog(@"Parsed Data ===>>>%@",parsedObject);
                                            }
                                      }];
    [dataTask resume];
}

在这段代码中,我正在使用

import "OAMutableURLRequest.h"

import "OAConsumer.h"

打印解析后的数据时我得到了**

    Parsed Data ===>>>{
    messages =     {
        error =         (
                        {
                code = 401;
                message = "oauth_problem=signature_invalid";
            }
        );
    };
}

任何人都可以帮助我吗?当我查看http响应代码时,我得到了401.我已经联系了服务提供商,了解了意外的401响应。

与此同时,对于我可能做错的事情,有什么能让你突出的吗?

即使只是向我指出一些工作示例代码也会受到高度赞赏。我已经阅读了很多东西,但我显然在obj-c中遇到了一些细微差别。

1 个答案:

答案 0 :(得分:0)

重新创建OAConsumer密钥。