Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get access token when call in Paypal in iOS

I want to get access token from Make your first call in paypal

I am convert Curl to objective 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"

My Objective c code like these.....

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

Any one have idea what is the problem in these code?

like image 679
user3751537 Avatar asked Jan 10 '23 19:01

user3751537


1 Answers

Seeing references to ASIFormDataRequest, it looks like you're using ASIHTTPRequest which is no longer actively developed nor maintained since 2011.

I would suggest switching to a more recent and maintained library or using Apple built-in libraries.

Using NSURLSession, a simple code to get an access token from Paypal would be:

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 would give you the following output containing your access token:

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;
}
like image 120
HiDeo Avatar answered Jan 18 '23 01:01

HiDeo