Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static String tenantId = "xxxxxxxxxxxxxxxxxxxxxxx";
- static String username = "xxxxxxxxxxxxxxxxxxxxxxx";
- static String password = "xxxxxxxxx";
- static String clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
- static String resource = "https://dev.sharepoint.com";
- static String userEmail = "xxxxxxxxxxxxxxxxxxxxxx";
- static String clientSecret = "xxxxxxxxxxxxxxxxxxx";
- public static void main(String[] args) throws MalformedURLException, IOException {
- AuthenticationContext authContext = null;
- AuthenticationResult authResult = null;
- ExecutorService service = null;
- try {
- service = Executors.newFixedThreadPool(1);
- String url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
- authContext = new AuthenticationContext(url, false, service);
- ClientCredential credential = new ClientCredential(clientId, clientSecret);
- Future<AuthenticationResult> future = authContext.acquireToken(resource, credential, null);
- authResult = future.get();
- System.out.println("get access token: n" + authResult.getAccessToken());
- System.out.println("get refresh token: n" + authResult.getRefreshToken());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- // get access token by refresh token
- getToken(authResult.getRefreshToken());
- }
- public static void getToken(String refreshToken) throws IOException {
- String encoding = "UTF-8";
- String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
- + "&grant_type=refresh_token&resource=https%3A%2F%2Fdev.sharepoint.com";
- String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
- byte[] data = params.getBytes(encoding);
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- //conn.setRequestProperty("Content-Length", String.valueOf(data.length));
- conn.setConnectTimeout(5 * 1000);
- OutputStream outStream = conn.getOutputStream();
- outStream.write(data);
- outStream.flush();
- outStream.close();
- System.out.println(conn.getResponseCode());
- System.out.println(conn.getResponseMessage());
- BufferedReader br = null;
- if (conn.getResponseCode() != 200) {
- br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
- } else {
- br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
- }
- System.out.println("Response body : " + br.readLine());
- }
Add Comment
Please, Sign In to add comment