如何通过 iOS 中的 API 调用获取 PayPal 刷新令牌
Posted
技术标签:
【中文标题】如何通过 iOS 中的 API 调用获取 PayPal 刷新令牌【英文标题】:How to get the PayPal refresh token through API call in iOS 【发布时间】:2016-09-15 19:51:17 【问题描述】:我已经使用 PayPal ios Sdk 成功实现了个人资料共享选项。 一旦用户在应用程序中登录到贝宝帐户,我就会得到正确的代码。 我尝试使用 curl 命令获取用户信息,我成功了。
现在我想通过 api 调用实现第二步和第三步。
以下是我为从 PayPal 服务器获取刷新令牌而实现的。
func getTheRefreshToken(authToken:NSString)
print("Token \(authToken)")
let urlPath: String = "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice"
let url: NSURL = NSURL(string: urlPath)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
let basicAuthCredentials: String = "AXvaZH_Bs9**CLIENTID**0RbhP0G8Miw-y:ED_xgio**SECRET**YFwMOWLfcVGs"
let plainData = (basicAuthCredentials as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let base64String = "Basic \(plainData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)))"
request.HTTPMethod = "POST"
let params = ["grant_type":"authorization_code","redirect_uri":"urn:ietf:wg:oauth:2.0:oob", "authorization_code":authToken as String] as Dictionary<String, String>
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue(base64String, forHTTPHeaderField: "Authorization")
request.timeoutInterval = 60
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
request.HTTPShouldHandleCookies=false
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) (response: NSURLResponse?, data: NSData?, error: NSError?) in
let refreshResponse = NSString(data: data!, encoding: NSISOLatin1StringEncoding)
print("Response \(refreshResponse!)")
每次我收到错误,grant_type 为 null。
错误
Response "error_description":"Grant type is null","error":"invalid_grant","correlation_id":"e5d4cc9c47d21","information_link":"https://developer.paypal.com/docs/api/#errors"
【问题讨论】:
【参考方案1】:这里有几件事... 1. 出于安全原因,您永远不应该将您的客户端 Secret 存储在客户端。 2. 您可以尝试使用 curl 命令大纲 here 从您的服务器调用并告诉我结果吗?
我可以从我们的内部日志中看到的唯一内容与缺少错误或授权类型相同。从您的服务器运行测试,使用响应中的授权代码,应该让我们知道它是否只是您的代码中的某些内容变得混乱。
【讨论】:
我已经完成了 curl 命令,它给了我一个正确的结果。我只想跳过服务器端集成。我已经解决了这个问题。感谢您的帮助:)【参考方案2】:使用此代码,您可以在 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];
这将给出这个响应。
data =
"access_token" = "A101.S6WF1CZIz9TcamYexl6k1mBsXhxEL1OWtotHq37UVHDrK7roty_4DweKXMhObfCP.7hNTzK62FqlDn3K9bqCjUIFmsVy";
"app_id" = "APP-80W284485P519543T";
"expires_in" = 32042;
nonce = "2016-12-26T10:24:12Z8qEQBxdSGdAbNMg2ivVmUNTUJfyFuSL30OI_W9UCgGA";
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/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*";
"token_type" = Bearer;
【讨论】:
以上是关于如何通过 iOS 中的 API 调用获取 PayPal 刷新令牌的主要内容,如果未能解决你的问题,请参考以下文章
如何通过iOS中的Twitter API获取用户电子邮件地址?
如何使用html和jquery通过asp.net核心api调用获取数据?
如何通过 API 调用从 azure 容器中获取 blob 数据?
即使App被IOS中的用户杀死,如何每10秒调用一次api?