我想从 Make your first call in paypal 获取访问 token
我正在将 Curl 转换为 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"
我的 Objective 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];
有人知道这些代码中的问题是什么吗?
最佳答案
看到对 ASIFormDataRequest
的引用,看起来您正在使用 ASIHTTPRequest
自 2011 年以来不再积极开发或维护。
我会建议切换到更新和维护的库或使用 Apple 内置库。
使用 NSURLSession
,从 Paypal 获取访问 token 的简单代码是:
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 将为您提供以下包含访问 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;
}
关于ios - 在 iOS 中调用 Paypal 时无法获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25866300/