-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetPhoneDataExample.java
More file actions
110 lines (96 loc) · 4.59 KB
/
GetPhoneDataExample.java
File metadata and controls
110 lines (96 loc) · 4.59 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2025 NFON AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// NFON CTI API GET example: Retrieve phone extensions
//
// What it does:
// 1. Logs in with API username and password to obtain an access token
// 2. Uses the token to send a GET request to retrieve phone extensions data
//
// Steps to run:
// 1. Set environment variables:
// Linux/macOS: export NFON_API_USERNAME='<YOUR API USERNAME>'
// export NFON_API_PASSWORD='<YOUR API PASSWORD>'
// Windows CMD: set NFON_API_USERNAME=<YOUR API USERNAME>
// set NFON_API_PASSWORD=<YOUR API PASSWORD>
// Windows PowerShell: $env:NFON_API_USERNAME='<YOUR API USERNAME>'
// $env:NFON_API_PASSWORD='<YOUR API PASSWORD>'
// 2. Run: java GetPhoneDataExample.java
//
// Requirements:
// - Java 11+
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class GetPhoneDataExample {
// TODO: Change these values to match your application
private static final String APP_NAME = "NFON-GitHub-Example"; // Replace with your application name
private static final String APP_VERSION = "1.0"; // Replace with your application version
private static final String USER_AGENT = APP_NAME + "/" + APP_VERSION;
private static final String USERNAME = System.getenv("NFON_API_USERNAME");
private static final String PASSWORD = System.getenv("NFON_API_PASSWORD");
public static void main(String[] args) {
try {
if (USERNAME == null || USERNAME.isEmpty() || PASSWORD == null || PASSWORD.isEmpty()) {
System.err.println("Error: NFON_API_USERNAME and NFON_API_PASSWORD must be set");
System.exit(1);
}
String token = getAccessToken();
System.out.println("Access token retrieved successfully.");
String extensions = getPhoneExtensionsData(token);
System.out.println("Phone extensions data: " + extensions);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getAccessToken() throws Exception {
URL url = URI.create("https://providersupportdata.cloud-cfg.com/v1/login").toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setDoOutput(true);
String jsonInput = String.format("{\"username\":\"%s\", \"password\":\"%s\"}", USERNAME, PASSWORD);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = jsonInput.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line.trim());
}
return response.toString().replaceAll(".*\"access-token\"\\s*:\\s*\"([^\"]+)\".*", "$1");
}
private static String getPhoneExtensionsData(String token) throws Exception {
URL url = URI.create("https://providersupportdata.cloud-cfg.com/v1/extensions/phone/data").toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestProperty("User-Agent", USER_AGENT);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line.trim());
}
return response.toString();
}
}