在iOS上创建条带收件人时出错

时间:2015-01-26 02:15:53

标签: ios swift post

我正在使用此代码创建条带收件人:

let name = "John Doe"
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.stripe.com/v1/recipients")!)
let params = "\(stripeKey):&name=\(name)&type=individual&card=tok_15P1vqDxE2HbT5qyw3sI7tku"

    request.HTTPMethod = "POST"
    request.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) 
{ 
(url: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

    let string = NSString(data: data, encoding: NSUTF8StringEncoding)
    println(string)
}

收到此错误:

"You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer     YOUR_SECRET_KEY'). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/."

我该怎么办?

2 个答案:

答案 0 :(得分:1)

我和Stripe的工作人员交谈过,我能够正确地做到这一点。我忘记了我的代码是在标题上:

//Getting Recipient
let name = "John Doe"
let request = NSMutableURLRequest(URL: NSURL(string:"https://api.stripe.com/v1/recipients")!)
let stripeKey = "sk_test_XXX"
let params = "name=\(name)&type=individual&card=\(token.tokenId)"


request.setValue("Bearer \(stripeKey)", forHTTPHeaderField: "Authorization") //This is what I had to add

答案 1 :(得分:0)

您是否阅读过Stripe documentationsStripePublishableKey与您的API密钥相同。

// AppDelegate.m

#import "AppDelegate.h"
#import "Stripe.h"

NSString * const StripePublishableKey = @"pk_test_6pRNASCoBOKtIshFeQd4XMUh";

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [Stripe setDefaultPublishableKey:StripePublishableKey];
  return YES;
}

@end