在iOS中调用Paypal时无法获取访问令牌

时间:2014-09-16 10:24:43

标签: ios objective-c iphone cocoa-touch paypal

我希望通过PayPal进行首次通话来获取访问令牌

我将Curl转换为目标c

 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"

我的目标c代码就像这些......

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

    request.tag = 1010;
    [request setRequestMethod:@"POST"];

    [request setUsername:@"kclientID"];
    [request setPassword:@"kSecrestID"];


    [request addRequestHeader:@"Accept" value:@"application/json"];
     [request addRequestHeader:@"Accept-Language" value:@"en_US"];

    NSMutableData *data1 = [[NSMutableData alloc] initWithData:[[NSString stringWithFormat:@"grant_type=client_credentials"] dataUsingEncoding:NSUTF8StringEncoding]];

     [request setPostLength:[data1 length]];
    [request setPostBody:data1];



    [request setDelegate:self];
    [request setDidFinishSelector:@selector(uploadFinishedLocation:)];
    [request setDidFailSelector:@selector(uploadFailLocation:)];
    [request startAsynchronous];

任何人都知道这些代码中的问题是什么?

1 个答案:

答案 0 :(得分:9)

看到对ASIFormDataRequest的引用,看起来您正在使用自2011年以来不再积极开发或维护的ASIHTTPRequest

我建议切换到更新的和维护的库或使用Apple内置库。

使用NSURLSession,从Paypal获取访问令牌的简单代码是:

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];

with将为您提供包含访问令牌的以下输出:

data = {
    "access_token" = "YOUR_NEW_ACCESS_TOKEN";
    "app_id" = "APP-YOUR_APP_ID";
    "expires_in" = 28800;
    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" = Bearer;
}