Skip to content

Commit 1ae1a94

Browse files
committed
interactive browser on console
1 parent 489ddeb commit 1ae1a94

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.example'
6+
version '1.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testCompile group: 'junit', name: 'junit', version: '4.12'
14+
implementation project(':coreLibrary')
15+
implementation 'com.azure:azure-identity:1.1.0'
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include 'coreLibrary'
2+
3+
project(':coreLibrary').projectDir = new File("../../")
4+
5+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.io.IOException;
2+
import java.util.List;
3+
import java.util.stream.Collectors;
4+
import java.util.stream.Stream;
5+
6+
import com.azure.identity.DeviceCodeCredential;
7+
import com.azure.identity.DeviceCodeCredentialBuilder;
8+
import com.azure.identity.InteractiveBrowserCredential;
9+
import com.azure.identity.InteractiveBrowserCredentialBuilder;
10+
import com.microsoft.graph.authentication.*;
11+
import okhttp3.*;
12+
import com.microsoft.graph.httpcore.HttpClients;
13+
import com.microsoft.graph.exceptions.*;
14+
15+
public class interactiveBrowserSample {
16+
private final static String CLIENT_ID = "fa62d88a-02fa-4893-88b3-8e566c7ad6f5";
17+
private final static String AUTHORITY = "https://login.microsoftonline.com/common/";
18+
private final static List<String> SCOPE = Stream.of("user.read").collect(Collectors.toList());
19+
20+
public static void main(String args[]) throws Exception {
21+
getUserWithHttp();
22+
}
23+
24+
private static void getUserWithHttp() throws AuthenticationException{
25+
DeviceCodeCredential deviceCodeCredential = new DeviceCodeCredentialBuilder()
26+
.challengeConsumer(challenge -> {
27+
// lets user know of the challenge
28+
System.out.println(challenge.getMessage());
29+
})
30+
.clientId(CLIENT_ID)
31+
.authorityHost(AUTHORITY)
32+
.build();
33+
34+
// InteractiveBrowserCredential interactiveBrowserCredential = new InteractiveBrowserCredentialBuilder()
35+
// .clientId(CLIENT_ID)
36+
// .port(8765)
37+
// .build();
38+
39+
TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(deviceCodeCredential,SCOPE);
40+
OkHttpClient httpClient = HttpClients.createDefault(tokenCredentialAuthProvider);
41+
42+
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/").build();
43+
44+
httpClient.newCall(request).enqueue(new Callback() {
45+
@Override
46+
public void onResponse(Call call, Response response) throws IOException {
47+
String responseBody = response.body().string();
48+
// Your processing with the response body
49+
System.out.println(responseBody);
50+
}
51+
52+
@Override
53+
public void onFailure(Call call, IOException e) {
54+
e.printStackTrace();
55+
}
56+
});
57+
}
58+
59+
}

0 commit comments

Comments
 (0)