跳至內容

Setting up AWS SNS for IOS using Node JS

0

This is how I am importing the APNS certificate and private key.

const fs = require("fs");
const path = require("path");

// including APNS certificate file
const certificatePath = path.join(
    __dirname,
    "../../certificates/apns-certificate.p12"
);
const privateKeyPath = path.join(
    __dirname,
    "../../certificates/apns-private-key.p8"
);

// to read the files
async function readCertificateAndPrivateKey(certificatePath, privateKeyPath) {
    try {
        const certificate = await fs.promises.readFile(certificatePath);
        const privateKey = await fs.promises.readFile(privateKeyPath, "utf8");
        return [certificate, privateKey];
    } catch (err) {
        console.error(err);
        throw new Error("Failed to read certificate or private key" + err);
    }
}

This is how I am setting up Create Platform Application parameters

           // setting parameters for IOS
            const [certificate, privateKey] =
                await readCertificateAndPrivateKey(
                    certificatePath,
                    privateKeyPath
                );
            createPlatformApplicationParams.Name = "BeeMobileAppIOS";
            createPlatformApplicationParams.Platform = "APNS";//APNS_SANDBOX 
            createPlatformApplicationParams.Attributes = {
                PlatformCredential: privateKey,
                PlatformPrincipal: certificate
            };

       // to create platform application
        const platformAppData = await createPlatformApplication(
            sns,
            createPlatformApplicationParams
        );

But it is giving following error:

 '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: '694b59a5-9dfa-5757-8e7d-055f1489b77d',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Error: {
    Type: 'Sender',
    Code: 'InvalidParameter',
    Message: 'Invalid parameter: PlatformPrincipal is not a valid Apple certificate.',
    message: 'Invalid parameter: PlatformPrincipal is not a valid Apple certificate.'
  },
  RequestId: '694b59a5-9dfa-5757-8e7d-055f1489b77d',
  xmlns: 'http://sns.amazonaws.com/doc/2010-03-31/'
}

So I want to ask, can I use the p12 file as PlatformPrincipal? I tried the p12 file using AWS console and it worked, but when I implement this in Node JS, it is giving PlatformPrincipal is not a valid Apple certificate error.

After that, I converted p12 certificate into .pem format, and then I try to create Application Platform, but this time it throws a different error:

  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: '5fefe7bb-9001-54f9-ba3a-bb4141726344',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Error: {
    Type: 'Sender',
    Code: 'InvalidParameter',
    Message: 'Invalid parameter: Attributes Reason: Platform credentials are invalid',
    message: 'Invalid parameter: Attributes Reason: Platform credentials are invalid'
  },
  RequestId: '5fefe7bb-9001-54f9-ba3a-bb4141726344',
  xmlns: 'http://sns.amazonaws.com/doc/2010-03-31/'
}

Can anyone please let me know what mistakes I am making?

已提問 3 年前檢視次數 611 次
1 個回答
0

To resolve platform credential errors, Kindly follow the below steps.

Important: Before completing the following steps, check the provider certificate type in your Apple Developer account. Then, download the certificate.

  1. Open the Amazon SNS console.
  2. On the navigation pane, choose Push notifications.
  3. Choose Create platform application.
  4. For Application name, enter a name for your application.
  5. For Push notification platform, select Apple iOS/VoIP/Mac.
  6. In the Apple Credentials section, select the Used for development in sandbox check box if you're using a certificate of type Sandbox.

Important: You must not select this check box if you're using a certificate of type Sandbox & Production. Otherwise, you receive an error.

  1. For Push certificate type, select your type of certificate.
  2. Choose Choose file, and then select the certificate that you want to load credentials from.
  3. Choose Create platform application.

Reference - https://repost.aws/knowledge-center/sns-invalid-parameter-error-api-call

AWS
專家
已回答 3 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。