Apple App Store 服务器 Python 库
这是适用于 App Store 服务器 API 和 App Store 服务器通知 的 Python 服务器库。同时还提供 Swift、Node.js 和 Java 版本。
目录
安装
要求
- Python 3.7+
pip
pip install app-store-server-library
文档
从 App Store Connect 获取应用内购买密钥
要使用 App Store 服务器 API 或创建促销优惠签名,需要从 App Store Connect 下载签名密钥。要获取此密钥,您必须拥有管理员角色。转到"用户和访问">"集成">"应用内购买"。在这里,您可以创建和管理密钥,以及找到您的发行者 ID。使用密钥时,您还需要密钥 ID 和发行者 ID。
获取 Apple 根证书
从 Apple PKI 网站的 Apple 根证书部分下载并存储根证书。将这些证书作为数组提供给 SignedDataVerifier,以验证签名数据是否来自 Apple。
使用方法
API 使用
from appstoreserverlibrary.api_client import AppStoreServerAPIClient, APIException
from appstoreserverlibrary.models.Environment import Environment
private_key = read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # 实现可能会有所不同
key_id = "ABCDEFGHIJ"
issuer_id = "99b16628-15e4-4668-972b-eeff55eeff55"
bundle_id = "com.example"
environment = Environment.SANDBOX
client = AppStoreServerAPIClient(private_key, key_id, issuer_id, bundle_id, environment)
try:
response = client.request_test_notification()
print(response)
except APIException as e:
print(e)
验证使用
from appstoreserverlibrary.models.Environment import Environment
from appstoreserverlibrary.signed_data_verifier import VerificationException, SignedDataVerifier
root_certificates = load_root_certificates()
enable_online_checks = True
bundle_id = "com.example"
environment = Environment.SANDBOX
app_apple_id = None # 对于生产环境,必须提供 appAppleId
signed_data_verifier = SignedDataVerifier(root_certificates, enable_online_checks, environment, bundle_id, app_apple_id)
try:
signed_notification = "ey.."
payload = signed_data_verifier.verify_and_decode_notification(signed_notification)
print(payload)
except VerificationException as e:
print(e)
收据使用
from appstoreserverlibrary.api_client import AppStoreServerAPIClient, APIException, GetTransactionHistoryVersion
from appstoreserverlibrary.models.Environment import Environment
from appstoreserverlibrary.receipt_utility import ReceiptUtility
from appstoreserverlibrary.models.HistoryResponse import HistoryResponse
from appstoreserverlibrary.models.TransactionHistoryRequest import TransactionHistoryRequest, ProductType, Order
private_key = read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # 实现可能会有所不同
key_id = "ABCDEFGHIJ"
issuer_id = "99b16628-15e4-4668-972b-eeff55eeff55"
bundle_id = "com.example"
environment = Environment.SANDBOX
client = AppStoreServerAPIClient(private_key, key_id, issuer_id, bundle_id, environment)
receipt_util = ReceiptUtility()
app_receipt = "MI.."
try:
transaction_id = receipt_util.extract_transaction_id_from_app_receipt(app_receipt)
if transaction_id != None:
transactions = []
response: HistoryResponse = None
request: TransactionHistoryRequest = TransactionHistoryRequest(
sort=Order.ASCENDING,
revoked=False,
productTypes=[ProductType.AUTO_RENEWABLE]
)
while response == None or response.hasMore:
revision = response.revision if response != None else None
response = client.get_transaction_history(transaction_id, revision, request, GetTransactionHistoryVersion.V2)
for transaction in response.signedTransactions:
transactions.append(transaction)
print(transactions)
except APIException as e:
print(e)
促销优惠签名创建
from appstoreserverlibrary.promotional_offer import PromotionalOfferSignatureCreator
import time
private_key = read_private_key("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8") # 实现可能会有所不同
key_id = "ABCDEFGHIJ"
bundle_id = "com.example"
promotion_code_signature_generator = PromotionalOfferSignatureCreator(private_key, key_id, bundle_id)
product_id = "<product_id>"
subscription_offer_id = "<subscription_offer_id>"
application_username = "<application_username>"
nonce = "<nonce>"
timestamp = round(time.time()*1000)
base64_encoded_signature = promotion_code_signature_generator.create_signature(product_id, subscription_offer_id, application_username, nonce, timestamp)
支持
只有最新的主要版本库会收到更新,包括安全更新。因此,建议更新到新的主要版本。