-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendTPInvites.java
113 lines (84 loc) · 4.55 KB
/
SendTPInvites.java
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
111
112
113
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SendTPInvites {
public static void main(String[] args) {
try {
StringBuilder buf = new StringBuilder();
buf.append(URLEncoder.encode("grant_type", "UTF-8")).append("=").append(URLEncoder.encode("password", "UTF-8"));
buf.append("&").append(URLEncoder.encode("username", "UTF-8")).append("=").append(URLEncoder.encode("TRUSTPILOT_B2B_EMAIL", "UTF-8"));
buf.append("&").append(URLEncoder.encode("password", "UTF-8")).append("=").append(URLEncoder.encode("TRUSTPILOT_B2B_PASSWORD", "UTF-8"));
String unit = "TRUSTPILOT_BUSINESS_UNIT_ID";
String key = "TRUSTPILOT_API_KEY";
String secret = "TRUSTPILOT_SECRET";
String encoded = Base64.encode((key + ":" + secret).getBytes());
URL u = new URL("https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken");
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + encoded);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(buf.length()));
OutputStream os = conn.getOutputStream();
os.write(buf.toString().getBytes());
os.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sbuf = new StringBuilder();
String line;
while((line = rd.readLine()) != null) {
sbuf.append(line);
}
os.close();
rd.close();
conn.disconnect();
JSONObject authJson = new JSONObject(sbuf.toString());
System.out.println((String)authJson.get("access_token"));
JSONObject json = new JSONObject();
json.put("recipientEmail", "[email protected]");
json.put("recipientName", "Amandeep Midha");
json.put("referenceId", "ORDER_ID");
json.put("templateId", "TRUSTPILOT_KS_TEMPLATE_ID");
json.put("locale", "en-GB");
json.put("senderEmail", "[email protected]");
json.put("senderName", "MY_NAME");
json.put("replyTo", "MY_REPLY_TO_ADDRESS");
json.put("preferredSendTime", "2015-03-13T19:12:00");
json.put("redirectUri", "http://trustpilot.com");
System.out.println(json.toString());
u = new URL("https://invitations-api.trustpilot.com/v1/private/business-units/" + unit + "/invitations");
conn = (HttpURLConnection)u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + (String)authJson.get("access_token"));
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(json.toString().length()));
os = conn.getOutputStream();
os.write(json.toString().getBytes());
os.flush();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sbuf = new StringBuilder();
while((line = rd.readLine()) != null) {
sbuf.append(line);
}
System.out.println(sbuf.toString());
os.close();
rd.close();
conn.disconnect();
} catch (UnsupportedEncodingException ex) {
System.out.println("UnsupportedEncodingException: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("IOException" + ex.getMessage());
} catch (JSONException ex) {
System.out.println("JSONException" + ex.getMessage());
}
}
}