|
| 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