2016-05-19 5 views
0

PayPalのiOS SDKでプロファイル共有オプションを正常に実装しました。 ユーザーがアプリのPayPalアカウントにログインすると、適切なコードを取得しています。 私はとユーザー情報を取得しようとしましたコマンドが成功しました。iOSのAPIコールを使用してPayPalリフレッシュトークンを取得する方法

今、私は第2ステップと第3ステップを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"} 

答えて

0

ここではカップルの事... 1。 2. curlコマンドの概要hereを使用してサーバーからの呼び出しを試行できますか?その結果を教えてください。

内部ログからわかるのは、エラーまたはgrant_typeが見つからない場合と同じです。レスポンスの認証コードを使用してサーバーからテストを実行すると、コード内に解体されているものがあるかどうかがわかります。

+0

私はすでにcurlコマンドを実行しており、正しい結果を得ています。私はちょうどサーバー側の統合をスキップしたい。私はすでに問題を解決しました。ご協力いただきありがとうございます :) – Hiren

0

このコードを使用すると、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; 
    } 
関連する問題