2016-05-19 6 views
0

Ich habe Profilfreigabe Option mit PayPal iOS Sdk erfolgreich implementiert. Ich bekomme richtigen Code, sobald Benutzer in Paypal-Konto in der App angemeldet. Ich habe versucht, die Benutzerinformationen mit curl Befehl zu erhalten Ich habe Erfolg.Wie bekomme ich das PayPal-Refresh-Token durch API-Aufruf in iOS

Jetzt möchte ich 2. und 3. Schritt durch API-Aufruf implementieren.

Unten ist, was ich implementiert habe, um Aktualisierungstoken von PayPal-Server zu erhalten.

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!)") 
     } 
    } 

Jedes Mal bekomme ich den Fehler mit grant_type als null.

Fehler

Response {"error_description":"Grant type is null","error":"invalid_grant","correlation_id":"e5d4cc9c47d21","information_link":"https://developer.paypal.com/docs/api/#errors"} 

Antwort

0

Ein paar Dinge hier ... 1. Sie sollten niemals Ihr Client Geheimnis auf der Client-Seite aus Sicherheitsgründen gespeichert haben. 2. Können Sie versuchen, den Anruf von Ihrem Server mit Hilfe der Curl-Befehle Gliederung here und lassen Sie mich wissen, das Ergebnis?

Das einzige, was ich aus unseren internen Logs sehen kann, ist das gleiche wie der Fehler oder grant_type fehlt. Wenn Sie den Test von Ihrem Server aus ausführen und dabei den Autorisierungscode in der Antwort verwenden, sollten Sie uns wissen lassen, ob nur etwas in Ihrem Code durcheinander gebracht wird.

+0

Ich habe bereits den Curl-Befehl gemacht und er gibt mir ein korrektes Ergebnis. Ich möchte nur die serverseitige Integration überspringen. Ich habe das Problem bereits gelöst. Danke für Ihre Hilfe :) – Hiren

0

Mit diesem Code können Sie aktualisieren oder neue Access-Token auf PayPal erhalten.

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

Dies wird diese Antwort geben.

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