Subscribing to a Topic
Function
This API is used to confirm the subscription to a topic.
Calling Method
For details, see Calling APIs.
Authorization Information
Each account has all the permissions required to call all APIs, but IAM users must be assigned the required permissions.
- If you are using role/policy-based authorization, see Permissions Policies and Supported Actions for details on the required permissions.
- If you are using identity policy-based authorization, no identity policy-based permission required for calling this API.
URI
GET /v2/notifications/subscriptions/subscribe
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| topic_urn | No | String | Specifies the unique resource identifier of a topic. To obtain the resource identifier, see Querying Topics. When the subscription type is SMS or email, this parameter is used together with the token and endpoint parameters. You can set any two or all of the three parameters to confirm a subscription. For other subscription types, this parameter is mandatory and must be used together with the token parameter. |
| endpoint | No | String | Specifies the subscription endpoint address. This parameter is available only when the subscription type is SMS or email. It is used together with the token and topic_urn parameters. You can set any two or all of the three parameters to confirm a subscription. |
| token | Yes | String | Specifies a token for subscription confirmation. (token information carried in a subscription link) |
Request Parameters
None
Response Parameters
Status code: 200
| Parameter | Type | Description |
|---|---|---|
| request_id | String | Unique request ID. |
| subscription_urn | String | Unique resource identifier of a subscriber. |
Status code: 400
| Parameter | Type | Description |
|---|---|---|
| message | String | Response information. |
Status code: 403
| Parameter | Type | Description |
|---|---|---|
| message | String | Response information. |
Status code: 404
| Parameter | Type | Description |
|---|---|---|
| message | String | Response information. |
Status code: 500
| Parameter | Type | Description |
|---|---|---|
| message | String | Response information. |
Example Requests
Subscribing to a topic
GET https://{SMN_Endpoint}/v2/notifications/subscriptions/subscribe?topic_urn=urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_topic_v2&token=b02df9b03fb84ace995caa6a21c56326b8c9f9e6f3734f2695f3ed2234edbfd90e6d046e93264bd083356abd46bc1217daa483eaf46c469883f91871928bddc6493f34dd49cf4ea2851af5f49bf95297 Example Responses
Status code: 200
{
"request_id" : "a10468986ae745f0b3b17683d872f2ef",
"subscription_urn" : "urn:smn:regionId:240489644ad14ace8e022869d34411a4:test11:b46ac8ad72104818bab55fe5f361e5ca"
} SDK Sample Code
The SDK sample code is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.smn.v2.region.SmnRegion; import com.huaweicloud.sdk.smn.v2.*; import com.huaweicloud.sdk.smn.v2.model.*; public class SubscribeTopicSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); SmnClient client = SmnClient.newBuilder() .withCredential(auth) .withRegion(SmnRegion.valueOf("<YOUR REGION>")) .build(); SubscribeTopicRequest request = new SubscribeTopicRequest(); try { SubscribeTopicResponse response = client.subscribeTopic(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdksmn.v2.region.smn_region import SmnRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdksmn.v2 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] credentials = BasicCredentials(ak, sk) client = SmnClient.new_builder() \ .with_credentials(credentials) \ .with_region(SmnRegion.value_of("<YOUR REGION>")) \ .build() try: request = SubscribeTopicRequest() response = client.subscribe_topic(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" smn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := smn.SmnClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := smn.NewSmnClient(hcClient) request := &model.SubscribeTopicRequest{} response, err := client.SubscribeTopic(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } } |
For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.
Status Codes
| Status Code | Description |
|---|---|
| 200 | OK |
| 400 | Bad Request |
| 403 | Unauthorized |
| 404 | Not Found |
| 500 | Internal Server Error |
Error Codes
See Error Codes.
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