2013-12-14 3 views
3

에 거래 ID를 얻기 위해 나는 응답 아래는있어?어떻게 아이폰 OS 페이팔 SDK

는 페이팔 REST API를 사용하여 는 TransactionId를 얻을 수있는 두 가지 단계가 있습니다, 1. AccessToken 2. AccessToken을 얻고 는 TransactionId를 얻을 수을 payment_id.

AccessToken을 얻으려면 아래에 몇 가지 참조 코드가 주어집니다. 마지막 두 줄에 대해 Objective-C로 코드를 변환하는 데 도움이 필요합니다. 헤더 (-H)를 알고 있지만 모르겠습니다. u는, 나는 아래의 코드를 사용

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \ 
    -H "Accept: application/json" \ 
    -H "Accept-Language: en_US" \ 
    -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \ 
    -d "grant_type=client_credentials" 

을 -d하지만 오류주고있다 :.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.sandbox.paypal.com/v1/oauth2/token"]]; 
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url]; 
    [theRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [theRequest addValue:@"Accept-Language" forHTTPHeaderField:@"en_US"]; 
    [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    NSString *strClientIdAndSecretKey=[NSString stringWithFormat:@"%@:%@",kPayPalClientId,kPayPalSecret]; 

[theRequest setValue:strClientIdAndSecretKey forHTTPHeaderField:@"client_id:secret"]; 

    NSString *parameterString = [NSString stringWithFormat:@"grant_type=client_credentials"]; 

    NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]]; 

    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 

    //do post request for parameter passing 
    [theRequest setHTTPMethod:@"POST"]; 

    [theRequest setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSLog(@"Headers %@",[theRequest allHTTPHeaderFields]); 

    [NSURLConnection sendAsynchronousRequest:theRequest 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *dataOrder, NSError *error) 
    { 


     if(error == nil) 
     { 
      NSString *jsonString = [[[NSString alloc] initWithData:dataOrder encoding:NSUTF8StringEncoding] autorelease]; 
      NSLog(@"Result is %@",jsonString); 
     } 
     else 
     { 

     } 
    } 
    ]; 

오류 도메인 = NSURLErrorDomain 코드 = -1012 "작업을 완료 할 수 없습니다 (NSURLErrorDomain를 오류 -1012). "UserInfo = 0xabd2bf0 {NSErrorFailingURLKey = https://api.sandbox.paypal.com/v1/oauth2/token, NSErrorFailingURL StringKey = https://api.sandbox.paypal.com/v1/oauth2/token, NSUnderlyingError = 0xabcf950 "작업을 완료 할 수 없습니다. (kCFErrorDomainCFNetwork 오류 -1012.) "}이 문제를 해결하기 위해, 어떤 아이디어 클라이언트 ID와 비밀을 통과하면서

내가 문제를 느낌?

+2

참조 링크 : https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/#get-an-access-token –

+1

안녕, 나는 동일한 문제가있는 경우 해당 URL을 사용하는 방법을 게시 코드로 알려주시겠습니까? – Minakshi

답변

2

이 코드

NSData * credential = [strClientIdAndSecretKey dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *credentialString = [credential base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]; 
[theRequest setValue:[NSString stringWithFormat:@"Basic %@",credentialString] forHTTPHeaderField:@"Authorization"];   

을 대체 자격 증명을 설정하려면

012 :

[theRequest setValue:strClientIdAndSecretKey forHTTPHeaderField:@"client_id:secret"]   
+0

시도해보십시오. 유효한 응답을 반환합니다. – user3601599

0

당신은 실수로 뒤집힌이 값이

이 있어야한다 :

[theRequest addValue:@"en_US" forHTTPHeaderField:@"Accept-Language"];

1

당신이 수 plz 수 볼 나는 그것을 당신에게 도움이 될 것입니다 희망 다음 코드 ..

Objective-C 소스 코드 :

NSString *clientID = @"YOUR_CLIENT_ID"; 
NSString *secret = @"YOUR_SECRET"; 

NSString *authString = [NSString stringWithFormat:@"%@:%@", clientID, secret]; 
NSData * authData = [authString dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *credentials = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]]; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
[configuration setHTTPAdditionalHeaders:@{ @"Accept": @"application/json", @"Accept-Language": @"en_US", @"Content-Type": @"application/x-www-form-urlencoded", @"Authorization": credentials }]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.sandbox.paypal.com/v1/oauth2/token"]]; 
request.HTTPMethod = @"POST"; 

NSString *dataString = @"grant_type=client_credentials"; 
NSData *theData = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:theData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
if (!error) { 
    NSLog(@"data = %@", [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]); 
} 
}]; 

[task resume]; 

응답 ::

data = { 
"access_token" = "YOUR_NEW_ACCESS_TOKEN"; 
"app_id" = "APP-YOUR_APP_ID"; 
"expires_in" = 34400; 
scope = "https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/services/invoicing https://api.paypal.com/v1/vault/credit-card/.*"; 
"token_type" = "YOUR_Token_Type"; 
}