AppStoreServerAPI - fancyfsz/FancyWiki GitHub Wiki

1.在AppStoreConnect即苹果后台创建API key

Store your private keys in a secure place. Don’t share your keys, don’t store keys in a code repository, and don’t include keys in client-side code. If you suspect a private key is compromised, immediately revoke the key in App Store Connect. See Revoking API Keys for details.

注意:这个key务必保存在一个安全的地方,不要添加到代码仓库里面去。

  • Select Users and Access, and then select the Keys tab.
  • Select In-App Purchase under the Key Type.
  • Click Generate API Key or the Add (+) button.
  • Enter a name for the key. The name is for your reference only and isn’t part of the key itself.
  • Click Generate.

具体路径就是:用户和访问 -> 密钥 -> App内购买项目。添加一个名称就可以加一个key,key只能下载一次,确认好了再点击下载按钮。

2.进行API请求需要构建一个JWT Token

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a way to securely transmit information. The App Store Server API requires a JWT to authorize each request you make to the API. You create the token, signing it with the private key you downloaded from App Store Connect.

用JWT Token的目的其实就是为了给每个请求进行授权

To generate a signed JWT:

  • Create the JWT header.
  • Create the JWT payload.
  • Sign the JWT.

其实也就是三个部分的东西,header/payload/signature

header部分的字段有alg即加密算法,这里必须填ES256;kid即key id(在第一步生成的api key的地方查看); typ即类型,这里必须填JWT payload部分的字段有iss即Issuer(这个每个App Store Connect主体下有唯一一个这个值,在苹果后台的密钥界面查看即可);iat(token的生成时间UNIX time,单位是秒);exp(token的过期时间和iat一样UNIX time,单位是秒); aud这里必须填appstoreconnnect-v1; bid即bundle id是你的应用包名。

有一个点需要注意一下: 按官方文档的意思,exp和iat的间隔不能超过60分钟否则就是无效的,如果你设置的间隔超过了60分钟,会报401的未授权错误。

signature部分有很多第三方的开源库可以来操作,这里不再赘述。

生成使用AppStoreServerAPI的JWT的python示例