2011-10-22 10 views
3

私のTwistedサーバーのアップルストアサーバーで、inAppの購入からトランザクションの領収書を検証しようとしています。私は(SKPaymentTransaction *)transaction.transactionReceiptを私のアプリから私のサーバーに送ってきました。Twistedサーバーによるアップルストアのレシート検証

しかし、AppleサーバーにJSONオブジェクトを送信すると、Agent.request()からDeferredで未処理のエラーが発生します。私はApple Storeからの応答のためにポート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トレースバック最後の最後のコール): \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 import succeed'を追加して問題を解決しました。したがって、ポート443でリッスンする必要はありませんでした。 – richard

関連する問題