Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.Future;
  6.  
  7. import javax.naming.ServiceUnavailableException;
  8.  
  9. import com.microsoft.aad.adal4j.AuthenticationContext;
  10. import com.microsoft.aad.adal4j.AuthenticationResult;
  11.  
  12. public class Get {
  13.  
  14. private final static String AUTHORITY = "https://login.windows.net/common/oauth2/v2.0/authorize";
  15. private final static String CLIENT_ID = "29275...011a";
  16. private final static String RESOURCE = "https://graph.windows.net";
  17.  
  18.  
  19. public static void main(String[] args) throws Exception {
  20.  
  21. try (BufferedReader br = new BufferedReader(new InputStreamReader(
  22. System.in))) {
  23. System.out.print("Enter username: ");
  24. String username = br.readLine();
  25. System.out.print("Enter password: ");
  26. String password = br.readLine();
  27.  
  28. AuthenticationResult result = getAccessTokenFromUserCredentials(
  29. username, password);
  30. System.out.println("Access Token - " + result.getAccessToken());
  31. System.out.println("Refresh Token - " + result.getRefreshToken());
  32. System.out.println("ID Token - " + result.getIdToken());
  33. System.out.println("Expires in - " + result.getExpiresAfter());
  34. }
  35. }
  36.  
  37. private static AuthenticationResult getAccessTokenFromUserCredentials(
  38. String username, String password) throws Exception {
  39. AuthenticationContext context = null;
  40. AuthenticationResult result = null;
  41. ExecutorService service = null;
  42. try {
  43. service = Executors.newFixedThreadPool(1);
  44. context = new AuthenticationContext(AUTHORITY, false, service);
  45. Future<AuthenticationResult> future = context.acquireToken(RESOURCE, CLIENT_ID, username, password, null);
  46. result = future.get();
  47. } finally {
  48. service.shutdown();
  49. }
  50.  
  51. if (result == null) {
  52. throw new ServiceUnavailableException(
  53. "authentication result was null");
  54. }
  55. return result;
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement