Generating JWT Token for Apple API Requests
Has anyone successfully generated a valid token for Apple's App Store API using ColdFusion?
We are following the instructions from Apple and cannot get the damn thing to authenticate. Every call returns "401 Unauthorized ".
We're using a JWT CFC code found here the uses Java for signing the JWT.
The curious thing is, we've used this same CFC to successfully sign a JWT (slightly different payload) for use with Apple's MapKitJS API. But, for reasons unknown, we can't get the App Store API to authenticate.
Any help is appreciated.
Our code sample:
<cfsavecontent variable="ourP8Key">
-----BEGIN PRIVATE KEY-----
Our
Key
Data
Here
-----END PRIVATE KEY-----
</cfsavecontent>
<!--- 'kid' value is private key ID from App Store Connect --->
<cfset tokenHeader = {
"kid":"OURPRIVATEKEY"
}>
<!--- iss value: issuer ID from the Keys page in App Store Connect --->
<cfset tokenPayload = {
"iss":"OUR-ISS-ID",
"iat":now(),
"exp":dateAdd("n",20,now()),
"aud":"appstoreconnect-v1",
"bid":"com.our.app"
}>
<cfset jwt = createobject("component","cfc.jwt.jwt").init()>
<cfset token = jwt.encode(tokenPayload, ourP8Key, 'ES256',tokenHeader)>
<cfhttp url="https://api.storekit-sandbox.itunes.apple.com/inApps/v1/history/#transactionid#" method="get" result="apiRes">
<cfhttpparam type="header" name="Authorization" value="Bearer #token#">
</cfhttp>"

