1+ package com .microsoft .graph .functional ;
2+
3+ import com .google .gson .Gson ;
4+ import com .google .gson .GsonBuilder ;
5+ import com .google .gson .JsonObject ;
6+ import com .microsoft .graph .authentication .IAuthenticationProvider ;
7+ import com .microsoft .graph .core .DefaultClientConfig ;
8+ import com .microsoft .graph .core .IClientConfig ;
9+ import com .microsoft .graph .http .IHttpRequest ;
10+ import com .microsoft .graph .models .extensions .IGraphServiceClient ;
11+ import com .microsoft .graph .models .extensions .GraphServiceClient ;
12+ import com .microsoft .graph .core .Constants ;
13+
14+ import java .io .BufferedReader ;
15+ import java .io .InputStreamReader ;
16+ import java .io .OutputStreamWriter ;
17+ import java .net .HttpURLConnection ;
18+ import java .net .URL ;
19+
20+
21+ public class TestBase {
22+ private String clientId ;
23+ private String username ;
24+ private String password ;
25+ // Don't use password grant in your apps. Only use for legacy solutions and automated testing.
26+ private String grantType = "password" ;
27+ private String tokenEndpoint = "https://login.microsoftonline.com/common/oauth2/token" ;
28+ private String resourceId = "https%3A%2F%2Fgraph.microsoft.com%2F" ;
29+ private String accessToken = null ;
30+
31+ protected IGraphServiceClient graphClient = null ;
32+
33+ public TestBase ()
34+ {
35+ clientId = Constants .APPID ;
36+ username = Constants .USERNAME ;
37+ password = Constants .PASSWORD ;
38+
39+ GetAuthenticatedClient ();
40+ }
41+
42+ private void GetAuthenticatedClient ()
43+ {
44+ if (graphClient == null ) {
45+ try {
46+ accessToken = GetAccessToken ().replace ("\" " , "" );
47+ IAuthenticationProvider mAuthenticationProvider = new IAuthenticationProvider () {
48+ @ Override
49+ public void authenticateRequest (final IHttpRequest request ) {
50+ request .addHeader ("Authorization" ,
51+ "Bearer " + accessToken );
52+ }
53+ };
54+ IClientConfig mClientConfig = DefaultClientConfig .createWithAuthenticationProvider (mAuthenticationProvider );
55+
56+ graphClient = GraphServiceClient .builder ().fromConfig (mClientConfig ).buildClient ();
57+ }
58+ catch (Exception e )
59+ {
60+ throw new Error ("Could not create a graph client: " + e .getLocalizedMessage ());
61+ }
62+ }
63+ }
64+
65+ private String GetAccessToken ()
66+ {
67+
68+ try {
69+ URL url = new URL (tokenEndpoint );
70+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
71+ String line ;
72+ StringBuilder jsonString = new StringBuilder ();
73+
74+ conn .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8" );
75+ conn .setRequestMethod ("POST" );
76+ conn .setDoInput (true );
77+ conn .setDoOutput (true );
78+ conn .setInstanceFollowRedirects (false );
79+ conn .connect ();
80+ OutputStreamWriter writer = new OutputStreamWriter (conn .getOutputStream (), "UTF-8" );
81+ String payload = String .format ("grant_type=%1$s&resource=%2$s&client_id=%3$s&username=%4$s&password=%5$s" ,
82+ grantType ,
83+ resourceId ,
84+ clientId ,
85+ username ,
86+ password );
87+ writer .write (payload );
88+ writer .close ();
89+ try {
90+ BufferedReader br = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
91+ while ((line = br .readLine ()) != null ){
92+ jsonString .append (line );
93+ }
94+ br .close ();
95+ } catch (Exception e ) {
96+ throw new Error ("Error reading authorization response: " + e .getLocalizedMessage ());
97+ }
98+ conn .disconnect ();
99+
100+ JsonObject res = new GsonBuilder ().create ().fromJson (jsonString .toString (), JsonObject .class );
101+ return res .get ("access_token" ).toString ();
102+
103+ } catch (Exception e ) {
104+ throw new Error ("Error retrieving access token: " + e .getLocalizedMessage ());
105+ }
106+ }
107+ }
0 commit comments