在使用Swift的App Receipt验证中返回代码21002

时间:2014-07-29 06:14:39

标签: swift receipt-validation

我已将我的数据发送到服务器,希望验证收据并收到格式错误的响应(21002)。我发送给iTunes的json在JSONlint.org中验证,如下所示:

{"receipt-data":"MIIVHAYJKoZIhvcNAQcCoIIVDTCCFQkCAQExCzAJBgUrDgMCGgUAMIIEzQYJKoZIhvcNAQcBoIIEvgSCBLoxggS2MAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgEDAgEBBAMMATEwCwIBCwIBAQQDAgEAMAsCAQ4CAQEEAwIBYTALAgEPAgEBBAMCAQAwCwIBEAIBAQQDAgEAMAsCARkCAQEEAwIBAzAMAgEKAgEBBAQWAjQrMA0CAQ0CAQEEBQIDATiAMA0CARMCAQEEBQwDMS4wMA4CAQkCAQEEBgIEUDIzMTAYAgEEAgECBBAt7VRGy6HkL8GxL7ZCostRMBsCAQACAQEEEwwRUHJvZHVjdGlvblNhbmRib3gwHAIBBQIBAQQULxX47jCym5JTHsewbqxnmPRNn3MwHQIBAgIBAQQVDBNjb20ubW0yMTQuSW5BcHBEZW1vMB4CAQwCAQEEFhYUMjAxNC0wNy0yOVQwNjowODowM1owHgIBEgIBAQQWFhQyMDEzLTA4LTAxVDA3OjAwOjAwWjBKAgEHAgEBBEJ_kvTvyRgKF9B080GZL0zmZSR93EEtsEyPBqO6PVrSePmQeoK\/vS1k9\/uZdd5XuaQyISrVWC2DpjU6eC_wLlLvqnQwUAIBBgIBAQRI85FUTb1tsun7ZcnNO9jP0Ya3Jpr\/njNS_P11OJ9GD7gbIArIWohrpkX0VN\/kH6KDHUeVPRkug\/\/pQm7pF6LQxPZs_tu\/QZIxMIIBVQIBEQIBAQSCAUsxggFHMAsCAgasAgEBBAIWADALAgIGrQIBAQQCDAAwCwICBrACAQEEAhYAMAsCAgayAgEBBAIMADALAgIGswIBAQQCDAAwCwICBrQCAQEEAgwAMAsCAga1AgEBBAIMADALAgIGtgIBAQQCDAAwDAIC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AQUFAAOCAQEAXDaZTC14t_2Mm9zzd5vydtJ3ME\/BH4WDhRuZPUc38qmbQI4s1LGQEti_9HOb7tJkD8t5TzTYoj75eP9ryAfsfTmDi1Mg0zjEsb_aTwpr\/yv8WacFCXwXQFYRHnTTt4sjO0ej1W8k4uvRt3DfD0XhJ8rxbXjt57UXF6jcfiI1yiXV2Q\/Wa9SiJCMR96Gsj3OBYMYbWwkvkrL4REjwYDieFfU9JmcgijNq9w2Cz97roy\/5U2pbZMBjM3f3OgcsVuvaDyEO2rpzGU_12TZ\/wYdV2aeZuTJC_9jVcZ5_oVK3G72TQiQSKscPHbZNnF5jyEuAF1CqitXa5PzQCQc3sHV1ITGCAcswggHHAgEBMIGjMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECgwKQXBwbGUgSW5jLjEsMCoGA1UECwwjQXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMxRDBCBgNVBAMMO0FwcGxlIFdvcmxkd2lkZSBEZXZlbG9wZXIgUmVsYXRpb25zIENlcnRpZmljYXRpb24gQXV0aG9yaXR5AggYWUMhcnSc\/DAJBgUrDgMCGgUAMA0GCSqGSIb3DQEBAQUABIIBAKnNWp78s\/DqSbFvJPRKmKlyLOAF9ggqScx8akGGRd3fvhFyvkAU2KfkdKvvx6puVg6AFfzdZWU2DhAl1\/_W5FtJq\/8ZmCPeBjRKzT93i40kKKOErKwrrpk44oPL3oL4kiAFEHMGC1qmfiEahSRyQo0ALl4aPwSMKhk7yK3aVORgXucNedUwG8q7WCX\/qtheLqabHvgSvt9VU9T\/WDv4dEwIal_yOtsZ9i3\/cLouH9IU41b3HMUwD4azDC5eNk3ys\/iOcI4D7EXm9bn_Bl8mLFXJWveN68mv0f6CQvN5tXx1fWOzEW5BvQ5x\/ZUwElLmnPa05OUPBMZJveU56uo_q8g"}

知道我可能做错了吗?我的斯威夫特如下:

var receiptUrl = NSBundle.mainBundle().appStoreReceiptURL;
var receipt: NSData = NSData.dataWithContentsOfURL(receiptUrl, options: nil, error: nil)
var receiptdata:NSString = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)


    var requestContents:NSDictionary = ["receipt-data": receiptdata];

    var jsonString:NSData = NSJSONSerialization.dataWithJSONObject(requestContents, options:nil,error:nil);


    var dta:NSString = NSString(data: jsonString, encoding: NSUTF8StringEncoding)


    request.HTTPBody = dta.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) 

    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")


    // send the request
    NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)

4 个答案:

答案 0 :(得分:2)

此代码可以帮助您:

func validateRecipt(){
    var response: NSURLResponse?
    var error: NSError?

    var recuptUrl = NSBundle.mainBundle().appStoreReceiptURL
    var receipt: NSData = NSData(contentsOfURL:recuptUrl!, options: nil, error: nil)!



    //https://buy.itunes.apple.com/verifyReceipt
    var request = NSMutableURLRequest(URL: NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 10)

    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"
    var receiptdata:NSString = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.EncodingEndLineWithLineFeed)
    NSLog("%@",receiptdata)
    var payload:NSString = "{\"receipt-data\" : \"\(receiptdata)\"}"
    var payloadData = payload.dataUsingEncoding(NSUTF8StringEncoding)
    var err: NSError?

    request.HTTPBody = payloadData

    var task = session.dataTaskWithRequest(request, completionHandler:
        {data, response, error -> Void in
            var err: NSError?
            var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

            if(err != nil) {
                println(err!.localizedDescription)
                let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
                println("Error could not parse JSON: '\(jsonStr)'")
            }
            else {
                if let parseJSON = json {
                    println("Recipt \(parseJSON)")
                }
                else {
                    let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
                    println("Recipt Error: \(jsonStr)")
                }
            }
    })

    task.resume()

}

答案 1 :(得分:1)

您的base64收据数据损坏了某些内容。我尝试使用NSJSONSerialization.JSONObjectWithData(_:options:)直接解码该字符串以及NSData(base64EncodedString:options:),如果您没有通过.IgnoreUnknownCharacters,则会返回nil。

我在App Store应用程序收到的收据上测试了其余的代码,它运行正常,所以它必须只是收据数据 - 尝试生成新收据。

答案 2 :(得分:0)

我遇到了这个问题并发现了Base64编码中的+,在他们的服务器之旅中被替换为空格。在postdata中正确编码它们(如%2B)解决了这个问题。

答案 3 :(得分:0)

问题在于您的网址编码。我有同样的问题,这就是我在Swift 2.3中修复它的方法:

extension String {
    // correct URL encoding
    var URLEncoded: String {
        let allowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
        let allowedCharSet = NSCharacterSet(charactersInString: allowedCharacters)
        let encodedString = self.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharSet)
        return encodedString ?? self
    } 
}

然后在验证前使用yourReceipt.URLEncoded