Skip to content

Commit ebac1bd

Browse files
authored
Create sms-opt-out-api-java.md
1 parent 3e3977a commit ebac1bd

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: include file
3+
description: include file
4+
services: azure-communication-services
5+
author: dbasantes
6+
7+
ms.service: azure-communication-services
8+
ms.subservice: azure-communication-services
9+
ms.date: 12/06/2024
10+
ms.topic: include
11+
ms.custom: include file
12+
ms.author: dbasantes
13+
---
14+
15+
Get started with Azure Communication Services SMS Opt-out API by leveraging the following Java sample code.
16+
17+
## Prerequisites
18+
19+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
20+
- Java Development Kit (JDK) version 8 or above.
21+
- An active Communication Services resource and connection string. See [Create a Communication Services resource](https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).
22+
- An SMS-enabled telephone number. See [Get a phone number](https://learn.microsoft.com/azure/communication-services/quickstarts/telephony/get-phone-number).
23+
24+
## Sample code to use Opt-Out API
25+
26+
Below is a sample Java implementation demonstrating how to use the Opt-Out Management API to add, remove, or check opt-out entries programmatically.
27+
28+
29+
```java
30+
// Sample for Add action. Replace with Check or Remove as necessary.
31+
public class App
32+
{
33+
public static void main(String[] args) throws Exception
34+
{
35+
String connectionString = "endpoint=https://[CONTOSO].communication.azure.com/;accesskey=******";
36+
37+
OptOutRequest payload = new OptOutRequest();
38+
payload.from = "+15551234567";
39+
payload.recipients = new ArrayList<Recipient>();
40+
payload.recipients.add(new Recipient("+15550112233"));
41+
42+
SendOptOut(connectionString, payload);
43+
}
44+
45+
public static void SendOptOutAdd(String connectionString, OptOutRequest payload) throws Exception
46+
{
47+
String apiVersion = "2024-12-10-preview";
48+
49+
String[] arrOfStr = connectionString.split(";");
50+
String endpoint = arrOfStr[0].split("=")[1];
51+
String accessKey = arrOfStr[1].split("=")[1];
52+
String body = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(payload);
53+
String dateHeaderName = "x-ms-date";
54+
DateTimeFormatter headerDateFormat = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).withZone(ZoneId.of("GMT"));
55+
String dateHeader = headerDateFormat.format(Instant.now());
56+
String verb = "POST";
57+
URI uri = URI.create(endpoint + "sms/optouts:add?api-version==" + apiVersion);
58+
String hostName = uri.getHost();
59+
String pathAndQuery = uri.getPath() + "?" + uri.getQuery();
60+
String encodedHash = Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(body.getBytes(StandardCharsets.UTF_8)));
61+
String stringToSign = verb + '\n' + pathAndQuery + '\n' + dateHeader + ';' + hostName + ';' + encodedHash;
62+
Mac mac = Mac.getInstance("HmacSHA256");
63+
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(accessKey.getBytes()), "HmacSHA256");
64+
mac.init(secretKeySpec);
65+
String signature = Base64.getEncoder().encodeToString(mac.doFinal(stringToSign.getBytes()));
66+
String authHeader = "HMAC-SHA256 SignedHeaders=" + dateHeaderName + ";host;x-ms-content-sha256&Signature=" + signature;
67+
68+
HttpClient client = HttpClients.custom().build();
69+
HttpUriRequest request = RequestBuilder
70+
.post(uri)
71+
.setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
72+
.setHeader(dateHeaderName, dateHeader)
73+
.setHeader("x-ms-content-sha256", encodedHash)
74+
.setHeader("Authorization", authHeader)
75+
.setEntity(new StringEntity(body, "UTF-8"))
76+
.build();
77+
HttpResponse r = client.execute(request);
78+
HttpEntity entity = r.getEntity();
79+
}
80+
}
81+
82+
public class OptOutRequest
83+
{
84+
public String from;
85+
public ArrayList<Recipient> recipients;
86+
}
87+
88+
public class Recipient
89+
{
90+
public String to;
91+
}
92+
```

0 commit comments

Comments
 (0)