【问题标题】:Apple store receipt validation through Twisted server通过 Twisted 服务器验证 Apple 商店收据
【发布时间】:2011-10-22 06:37:08
【问题描述】:

我正在尝试使用我的 Twisted 服务器上的 Apple 商店服务器验证应用内购买的交易收据。我已将(SKPaymentTransaction *)transaction.transactionReceipt 从我的应用程序发送到我的服务器。

但是现在,将 JSON 对象发送到 Apple 服务器时,我不断收到来自 Agent.request() 的 Deferred 中未处理的错误。我怀疑这是因为我没有在端口 443 上监听 Apple 商店的响应,但我不希望我的应用程序也与端口 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 连接的服务通信?

【问题讨论】:

  • 你没有说你得到了什么错误,只是你得到了一个。您应该始终包含完整的错误文本(回溯和异常)。
  • 这是我得到的错误:2011-10-22 17:09:32-0400 [-] Unhandled error in Deferred: 2011-10-22 17:09:32-0400 [-] Unhandled Error Traceback (most recent call last): Failure: twisted.web._newclient.RequestGenerationFailed: [<twisted.python.failure.Failure <type 'exceptions.NameError'>>]

标签: iphone python https in-app-purchase twisted


【解决方案1】:

您的代码示例中的注释样式不正确。 Python 使用 # 表示 cmets,而不是 //。

修复该问题并通过 pyflakes 运行 sn-p 后,我看到以下错误:

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'

您遇到的NameError 似乎是第 21 行上未定义的名称的原因。 NameError 是 Python 发出这种错误信号的方式:

x = y

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

【讨论】:

  • 感谢您的更正。我在原始帖子中编辑了我的 python 评论单。我忘了在我的代码中包含application = service.Application("myServer"),所以使用了'service'defer 在另一个服务中使用过,所以这里确实不需要它。为了解决第 21 行,我添加了 from twisted.internet.defer import succeed 并解决了问题。所以没有必要监听 443 端口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多