通过Twisted s验证苹果商店收据

2024-04-18 19:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从我的Twisted服务器验证一个从inApp购买的交易凭证。我已将(SKPaymentTransaction *)transaction.transactionReceipt从我的应用程序发送到服务器。在

但是现在,将JSON对象发送到Apple服务器时,我的Agent.request()中不断出现未经处理的错误。我怀疑这是因为我没有监听端口443从苹果商店的响应,但我不想我的应用程序与我的Twisted服务器在端口443也通信。这是我的代码:

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连接的服务通信?在


Tags: 端口fromimportself服务器web应用程序data
1条回答
网友
1楼 · 发布于 2024-04-18 19:21:00

代码示例中的注释样式不正确。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如何向此类bug发出信号的:

^{pr2}$

相关问题 更多 >