2011-10-22 27 views
3

我試圖從Twisted服務器驗證來自Apple商店服務器的inApp購買交易收據。我已將(SKPaymentTransaction *)transaction.transactionReceipt從我的應用程序發送到我的服務器。通過Twisted服務器驗證蘋果商店收據

但現在,發送JSON對象到蘋果服務器,我不斷從我的Agent.request()推遲未處理的錯誤。我懷疑這是因爲我沒有在蘋果商店的響應端口443上偵聽,但我不希望我的應用程序也與端口443上的Twisted服務器通信。這裏是我的代碼:

from twisted.application import internet, service 
from twisted.internet import protocol, reactor 
from zope.interface import implements 
from twisted.web.iweb import IBodyProducer 

from twisted.internet import defer 
from twisted.web.client import Agent 
from twisted.web.http_headers import Headers 
import json 
import base64 

class StringProducer(object): 
    implements(IBodyProducer) 

    def __init__(self, body): 
     self.body = body 
     self.length = len(body) 

    def startProducing(self, consumer): 
     consumer.write(self.body) 
     return succeed(None) 

    def pauseProducing(self): 
     pass 

    def stopProducing(self): 
     pass 

def printResponse(response): 
    print response  # just testing to see what I have 

def httpRequest(url, values, headers={}, method='POST'): 
    agent = Agent(reactor) 
    d = agent.request(method, 
         url, 
         Headers(headers), 
         StringProducer(values) 
        ) 
    d.addCallback(printResponse) 

class storeServer(protocol.Protocol): 

    def dataReceived(self, data): 
     receiptBase64 = base64.standard_b64encode(data) 
     jsonReceipt = json.dumps({'receipt-data':receiptBase64}) 
     print jsonReceipt  # verified that my data is correct 

     d = httpRequest(
      "https://buy.itunes.apple.com/verifyReceipt", 
      jsonReceipt, 
      {'Content-Type': ['application/x-www-form-urlencoded']} 
      ) 

factory = protocol.Factory() 
factory.protocol = storeServer 
tcpServer = internet.TCPServer(30000, factory) 
tcpServer.setServiceParent(application) 

我該如何解決這個錯誤?我是否需要在端口443上創建另一個服務?如果是這樣,我該如何將連接到我的應用的服務與通過https連接的服務進行通信?

+1

你沒有說出你得到了什麼錯誤,只是你得到了一個。您應該始終包含完整的錯誤文本(追溯和異常)。 –

+0

這是我得到的錯誤:'2011-10-22 17:09:32-0400 [ - ]延遲中未處理的錯誤: 2011-10-22 17:09:32-0400 [ - ]未處理的錯誤 \t Traceback最近的呼叫最後): \t失敗:twisted.web._newclient.RequestGenerationFailed:[>]' – richard

回答

1

代碼示例中的註釋樣式不正確。 Python使用#來註釋,而不是//。

固定這一點,貫穿pyflakes的片斷之後,我看到這些錯誤:

program.py:1: 'service' imported but unused 
program.py:6: 'defer' imported but unused 
program.py:21: undefined name 'succeed' 
program.py:48: local variable 'd' is assigned to but never used 
program.py:57: undefined name 'application' 

這很可能是第21行的未定義的名稱是NameError您所遇到的原因。 NameError是Python如何發出這種錯誤的信號:

x = y 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'y' is not defined 
+0

感謝您的更正。我在原始文章中編輯了我的python評論單。我忘記在我的代碼中包含'application = service.Application(「myServer」)',所以使用'service'。 'defer'在另一個服務中使用,所以在這裏確實不需要。爲了解決第21行,我添加了'從twisted.internet.defer導入成功',並解決了這個問題。所以沒有必要聽443端口。 – richard