2016-02-11 24 views
0

我正在開發一個應用程序,需要使用OAuth1.0調用API。OAMutableURLRequest:簽名invalide

我可以成功獲取訪問令牌。但是當我嘗試發佈Json數據的時候。它給我錯誤。 帶請求的後JSON數據代碼。

-(void)sampleRequest2{ 
    // create URL from string 
    NSURL* url = [[NSURL alloc] initWithString:@"http://myurl"]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Json" ofType:@"rtf"]; 
    NSData *data = [NSData dataWithContentsOfFile:path]; 


    // create url request 
    OAMutableURLRequest *urlRequest = [[[OAMutableURLRequest alloc] initWithURL:url 
                consumer:consumer 
                 token:accessToken 
                 realm:nil 
              signatureProvider:nil] autorelease]; 
    OARequestParameter* callbackParam = [[OARequestParameter alloc] initWithName:@"oauth_callback" value:callback]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setParameters:[NSArray arrayWithObjects:callbackParam,nil]]; 
    [urlRequest prepare]; 


    __weak NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[data length]]; 

    // Create entire body 
    NSMutableData* dataRequestBody = [NSMutableData data]; 

    NSString* boundary = @"----IMAGE_UPLOAD"; 
    [dataRequestBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [dataRequestBody appendData:[[NSString stringWithFormat:@"Content-Length: %@\r\n\r\n",postLength] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [dataRequestBody appendData:[@"Content-Type: application/json\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [dataRequestBody appendData:data]; 
    [dataRequestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    [urlRequest setHTTPBody:dataRequestBody]; 
OADataFetcher *dataFetcher = [[OADataFetcher alloc]init]; 
    [dataFetcher fetchDataWithRequest:urlRequest delegate:self completionBlock:^(OAServiceTicket *ticket, NSMutableData *data) { 
     if (data == nil) { 

     }else{ 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      NSLog(@"JSON Response: %@",json); 
     } 
    } 
          failedBlock:^{ 
           NSLog(@"Failed"); 
          }]; 
} 

錯誤響應:

JSON Response: { 
    messages =  { 
     error =   (
         { 
       code = 401; 
       message = "oauth_problem=signature_invalid"; 
      } 
     ); 
    }; 
} 

回答

0

我終於找到了兩天後,搜索解決方案。

我需要在創建請求時傳遞Signature Provider。

OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:accessToken realm:realm signatureProvider:[[OAPlaintextSignatureProvider alloc] init]]; 

用於傳遞JSON數據。

[request setHTTPBody:[strJson dataUsingEncoding:NSUTF8StringEncoding]]; 

所以,壓軸代碼..

OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:accessToken realm:realm signatureProvider:[[OAPlaintextSignatureProvider alloc] init]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setParameters:[NSArray arrayWithObjects:callbackParam,releamParam, nil]]; 

    [request setValue:@"*/*" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    NSString *strJson = [NSString stringWithFormat:@"%@",@"{\"products\" : [ {\"qty\" : \"1\",\"product\" : \"404\",\"super_attribute\" : {\"180\" : \"80\",            \"92\" : \"17\" } }]}"]; 
    [request setHTTPBody:[strJson dataUsingEncoding:NSUTF8StringEncoding]]; 
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init]; 

    [dataFetcher fetchDataWithRequest:request delegate:self completionBlock:^(OAServiceTicket *ticket, NSMutableData *data) { 
     if (data == nil) { 

     }else{   
      NSLog(@"header: %@",request.allHTTPHeaderFields); 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      NSLog(@"JSON Response: %@",json); 
     } 
    } 
          failedBlock:^{ 
           NSLog(@"Failed"); 
          }];