forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBase.java
More file actions
106 lines (92 loc) · 3.96 KB
/
TestBase.java
File metadata and controls
106 lines (92 loc) · 3.96 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
package com.microsoft.graph.functional;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.core.Constants;
import com.microsoft.graph.core.DefaultClientConfig;
import com.microsoft.graph.core.IClientConfig;
import com.microsoft.graph.http.IHttpRequest;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.requests.extensions.GraphServiceClient;
public class TestBase {
private String clientId;
private String username;
private String password;
// Don't use password grant in your apps. Only use for legacy solutions and automated testing.
private String grantType = "password";
private String tokenEndpoint = "https://login.microsoftonline.com/common/oauth2/token";
private String resourceId = "https%3A%2F%2Fgraph.microsoft.com%2F";
private String accessToken = null;
protected IGraphServiceClient graphClient = null;
public TestBase()
{
clientId = Constants.APPID;
username = Constants.USERNAME;
password = Constants.PASSWORD;
GetAuthenticatedClient();
}
private void GetAuthenticatedClient()
{
if (graphClient == null) {
try {
accessToken = GetAccessToken().replace("\"", "");
IAuthenticationProvider mAuthenticationProvider = new IAuthenticationProvider() {
@Override
public void authenticateRequest(final IHttpRequest request) {
request.addHeader("Authorization",
"Bearer " + accessToken);
}
};
IClientConfig mClientConfig = DefaultClientConfig.createWithAuthenticationProvider(mAuthenticationProvider);
graphClient = GraphServiceClient.fromConfig(mClientConfig);
}
catch (Exception e)
{
throw new Error("Could not create a graph client: " + e.getLocalizedMessage());
}
}
}
private String GetAccessToken()
{
try {
URL url = new URL(tokenEndpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String line;
StringBuilder jsonString = new StringBuilder();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.connect();
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
String payload = String.format("grant_type=%1$s&resource=%2$s&client_id=%3$s&username=%4$s&password=%5$s",
grantType,
resourceId,
clientId,
username,
password);
writer.write(payload);
writer.close();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((line = br.readLine()) != null){
jsonString.append(line);
}
br.close();
} catch (Exception e) {
throw new Error("Error reading authorization response: " + e.getLocalizedMessage());
}
conn.disconnect();
JsonObject res = new GsonBuilder().create().fromJson(jsonString.toString(), JsonObject.class);
return res.get("access_token").toString().replaceAll("\"", "");
} catch (Exception e) {
throw new Error("Error retrieving access token: " + e.getLocalizedMessage());
}
}
}