apple / app-store-server-library-node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: secretOrPrivateKey must be an asymmetric key when using ES256 in Production environment

thomas2109 opened this issue · comments

Everything works fine in the Sandbox environment. But when I switch to production, with the same private key and other variables, I got this error

error: Error: secretOrPrivateKey must be an asymmetric key when using ES256
at Object.module.exports [as sign] (/opt/app/node_modules/@apple/app-store-server-library/node_modules/jsonwebtoken/sign.js:130:22)
at AppStoreServerAPIClient.createBearerToken (/opt/app/node_modules/@apple/app-store-server-library/index.ts:365:29)
at AppStoreServerAPIClient.makeRequest (/opt/app/node_modules/@apple/app-store-server-library/index.ts:111:47)
at AppStoreServerAPIClient.getTransactionHistory (/opt/app/node_modules/@apple/app-store-server-library/index.ts:311:27)
at findLastReceiptFromOriginalTransactionId (/opt/app/v1/helpers/appleHelpers.ts:386:49)

I found that it may be caused by the wrong format of the private key file or the newline character, but it still works in the Sandbox environment. I'm using fs-extra to read the p8 file

const encodedKey = fs.readFileSync('apple-api-key.p8', { encoding: 'utf8' });

Node v16.19.0

Hey @thomas2109, i am unable to running it sandbox as well. Here's my code:
const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); const encodedKey = path.join(_dirname, ../../utils/lib/dir, 'test.p8'); const client = new AppStoreServerAPIClient(encodedKey, keyId, issuerId, bundleId, environment);
What am i doing wrong here?
Thanks!

It seems that you just only defined that path of the file. You need to read to content of the file to extract the key

So Sandbox or Production doesn't affect the parsing at the p8, it is definitely possible if the format of the file was changed it could be throwing an error

But I kept reading the file content the same way as in Sandbox. Do you suggest any other way to import the private key in the correct format?

It seems that you just only defined that path of the file. You need to read to content of the file to extract the key

Oops. added the fs readFileSync now it's working thanks.

@thomas2109 just to confirm the below logs one success and one error?

const client = new AppStoreServerAPIClient(encodedKey, keyId, issuerId, bundleId, Environment.SANDBOX)
try {
    const response: SendTestNotificationResponse = await client.requestTestNotification()
    console.log("Success Sandbox")
} catch (e) {
    console.error(e)
}
const prodClient = new AppStoreServerAPIClient(encodedKey, keyId, issuerId, bundleId, Environment.PRODUCTION)
try {
    const response: SendTestNotificationResponse = await prodClient.requestTestNotification()
    console.log("Success Production")
} catch (e) {
    console.error(e)
}

@alexanderjordanbaker Both succeeded in my development environment.

Oh, you don't mean Production as in Environment.PRODUCTION you mean your production environment, I'm tracking now. Yeah, it sounds like the formatting of the p8 file differs between the environments. I recommend checking the line endings and testing with the same OS as production to rule out any OS differences.

@thomas2109 Were you able to resolve your issue?

@alexanderjordanbaker My task is pending for now. I'm try to import the key as a env variable instead of reading from a file. After some investigation, I think it may related to the /n character, which cause the wrong format of key issue

However, I'm not sure if this will work on production as my code hasn't been deployed yet.

@thomas2109 If you have additional issues, please open a new issue, and we'll be happy to take a look. For now, closing this issue due to inactivity