Getting Started
Scenarios
This topic describes how to publish a message to a topic by calling APIs.
For details about how to call APIs, see Calling APIs.
The token obtained from IAM is valid for only 24 hours. To reuse the same token for authentication, you can cache it to avoid frequent API calls.
APIs
If you use a token for authentication, you must obtain the token and add X-Auth-Token to the request header of each service API.
- IAM API to obtain a token
- SMN API to create a topic
- SMN API to add subscriptions
- SMN API to publish messages
Procedure
- Obtain the token by referring to Authentication.
- Send a request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics to create a topic.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
{ "name": "test_topic_v2", // Topic name (mandatory) "display_name": "test" // Display name (optional) }If the request is successful, the API returns a request ID (request_id) and topic URN (topic_urn).
If the request fails, the API returns an error code and error information. For details, see Error Codes.
- Send a request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/subscriptions to add subscriptions.
Set topic_urn based on the response in 2.b.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
{ "protocol": "email", // Subscription protocol (mandatory) "endpoint": "xxx@xx.com", // Email address (mandatory) "remark": "Subscription" // Description (optional) }If the request is successful, the API returns a request ID (request_id) and subscription URN (subscription_urn).
If the request fails, the API returns an error code and error information. For details, see Error Codes.
- The subscription endpoint receives a confirmation message and must confirm the subscription in the email.
- Send a request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/publish to publish a message.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
{ "subject":"test message v2", // Message subject, which is displayed in email messages (optional) "message": "Message test message v2." // Message to be published (mandatory) }If the request is successful, the API returns a request ID (request_id) and message ID (message_id).
If the request fails, the API returns an error code and error information. For details, see Error Codes.
Only confirmed subscription endpoints can receive the message you publish.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot