Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Parameter: Topic Name Error when publishing to AWS SNS arn endpoint

In Java, I am trying to publish a AWS SNS message to a specific ARN endpoint using the following code:

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;

...

AmazonSNS snsClient = new AmazonSNSClient(new BasicAWSCredentials(System.getenv("AWS_KEY"), System.getenv("AWS_SECRET")));

snsClient.setRegion(Region.getRegion(Regions.US_EAST_1));

String message = "{\"APNS_SANDBOX\":\"{\\\"aps\\\":{\\\"type\\\":\\\"XXX\\\",\\\"email\\\":\\\"[email protected]\\\",\\\"alert\\\":\\\"some alert\\\"}}\"}";

PublishResult pr = snsClient.publish(new PublishRequest("arn:aws:sns:us-east-1:XXX:endpoint/APNS_SANDBOX/XXX/XXX-XXX-XXX-XXX", message));

I am consistently getting the following error message:

Invalid parameter: Topic Name (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: XXX

Any idea on this?

I can publish to the ARN endpoint with no issues from the SNS console and I have tried different variations of the message

like image 792
Blaine Rothrock Avatar asked Jun 20 '16 23:06

Blaine Rothrock


3 Answers

You should use TargetArn instead of TopicArn which is the first parameter of PublishRequest constructor.

PublishRequest pr = new PublishRequest();
pr.TargetArn = "arn:aws:sns:us-east-1:XXX:endpoint/APNS_SANDBOX/XXX/XXX-XXX-XXX-XXX";
pr.Message = message;

(maybe it is too late to answer lol)

like image 190
Rex Lam Avatar answered Sep 21 '22 14:09

Rex Lam


The error message is a bit confusing. If you provide the wrong topic ARN, you get error message saying Invalid Parameter: Topic Name as well.

From what you posted, it looks like you might have used the subscription ARN, instead of topic ARN. Topic ARN format looks like this arn:aws:sns:AWS_REGION:AWS_ACCOUNT_NUMBER:TOPIC_NAME.

like image 23
chepukha Avatar answered Sep 21 '22 14:09

chepukha


Your problem was this: String message = "{"APNS_SANDBOX":"{\"aps\":{\"type\":\"XXX\",\"email\":\"[email protected]\",\"alert\":\"some alert\"}}"}";

instead it should have been: String message = "{"APNS_SANDBOX":"{\"aps\":{\"type\":\"XXX\",\"email\":\"[email protected]\",\"alert\":\"somealert\"}}"}";

You cant put name of the topic with spaces. So "some alert" will be rejected. "somealert" will be accepted.

In case someone else stumbles upon this problem.

like image 23
Slobodan Djipalo Avatar answered Sep 18 '22 14:09

Slobodan Djipalo