python3: 获取苹果到server的通知历史

代码:

import jwt
import time
import requests
import json

# issuer id: xxxx5efd-cxxd-4xx3-axx5-xx8axxxx9fxx
# bundle id:

issuer_id = "xxxx5efd-cxxd-4xx3-axx5-xx8axxxx9fxx"
bundle_id = "com.niubie.hao"
privatekey_path = "/data/work/python/SubscriptionKey_YZxx57xx74.p8"
privatekey_id = "YZxx57xx74"
# 读取密钥文件证书内容
f = open(privatekey_path)
key_data = f.read()
f.close()

# JWT Header
header = {
	"alg": "ES256",
	"kid": privatekey_id,
	"typ": "JWT"
}

# JWT Payload
payload = {
	"iss": issuer_id,
	"aud": "appstoreconnect-v1",
	"iat": int(time.time()),
	"exp": int(time.time()) + 60 * 60, # 60 minutes timestamp
	"nonce": "6edffe66-b482-11eb-8529-0242ac130003",
	"bid": bundle_id
}

# JWT token
token = jwt.encode(headers=header, payload=payload, key=key_data, algorithm="ES256")

print("JWT Token:", token)

# apple test push
# url = "https://api.storekit.itunes.apple.com/inApps/v1/notifications/test"
url = "https://api.storekit.itunes.apple.com/inApps/v1/notifications/history"

# url = "https://api.storekit.itunes.apple.com/inApps/v1/lookup/" + "MK5TTTVWJH"
header = {
	"Authorization": f"Bearer {token}",
	"Content-Type": "application/json"
}


# 得到时间
now = int(time.time())
endDate = now*1000     #现在
startDate = (now - 24*60*60*14)*1000

#endDate = now -24*60*60*1     #现在
#startDate = (now - 24*60*60*8)

print("now:",now)
print("startDate:",startDate)
print("endDate:",endDate)




# 得到参数
post_data = {
        'startDate': startDate,
        'endDate': endDate,
        'notificationType':'ONE_TIME_CHARGE',
    }
json_data = json.dumps(post_data)
# 请求和响应



response = requests.post(url, data=json_data,  headers=header)
print(response)
print("text:")
print(response.text)

response.encoding='utf-8'
print('状态码:',response.status_code)  # 打印状态码
print('url:', response.url)          # 打印请求url
print('header:',response.headers)      # 打印头信息
print('cookie:',response.cookies)      # 打印cookie信息
print('源码:',response.text)  #以文本形式打印网页源码
print('字节流形式打印:',response.content) #以字节流形式打印

 

posted @ 2024-12-28 09:35  刘宏缔的架构森林  阅读(3)  评论(0编辑  收藏  举报