【问题标题】:Can't get access token when call in Paypal in iOS在 iOS 中调用 Paypal 时无法获取访问令牌
【发布时间】:2014-11-10 01:22:18
【问题描述】:

我想从 Make your first call in 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"

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

有人知道这些代码有什么问题吗?

【问题讨论】:

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


    【解决方案1】:

    看到对 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;
    }
    

    【讨论】:

    • 我已根据您发布的答案成功获取访问令牌。你能告诉我如何使用objective-c进行以下CURL请求吗? curl -v https://api.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid \ -H "Content-Type:application/json" \ -H "Authorization: Bearer WfXdnxmyJtdF4q59ofxuQuAAk6eEV-Njm6puht3Nk3w"我正在努力实现这一目标:github.com/paypal/PayPal-iOS-SDK/blob/master/docs/…
    • 好吧,您可以遵循相同的模式,CURL 中的 -H 是标头,因此您必须提供新的 Content-Type (application/json) 并按照使用的语法添加带有令牌的 Authorization 字段在 curl 命令中。您可以删除 NSMutableURLRequest 上的 HTTPMethod 属性更改,因为它们默认为 GET,您可以从 NSURLSessionUploadTask 切换到 NSURLSessionDataTask
    • 为了使用 Objective-C 实现这一目标,我花了很多天的时间。非常感谢您。完成!!!
    • 为你节省了一天的时间
    猜你喜欢
    • 2019-02-17
    • 2017-07-30
    • 2020-01-06
    • 1970-01-01
    • 2017-03-22
    • 2021-11-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多