All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.cloud.chat.AzureMeetingService Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fivefaces.cloud.chat;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;


@Profile("CHAT_AZURE")
@Slf4j
@Service
public class AzureMeetingService implements MeetingService {

    private final RestTemplateBuilder restTemplateBuilder;
    private RestTemplate restTemplate;

    public AzureMeetingService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplateBuilder = restTemplateBuilder;
    }

    @Override
    public String getToken() {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://login.microsoftonline.com/db22c75a-5289-4193-9327-70732230ff28/oauth2/token");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("grant_type", "client_credentials");
        map.add("client_id", "4ee3d788-2170-4872-b331-081fe95dafda");
        map.add("client_secret", "cXW7Q~K.KQJt9ECiD5Zq1GfOAugXyctOcVzkC");
        map.add("resource", "https://graph.microsoft.com/");

        HttpEntity> request = new HttpEntity>(map, headers);

        ResponseEntity response = getOrCreateRestTemplate().postForEntity( builder.build().encode().toUri(), request , Map.class );

        return (String) response.getBody().get("access_token");

    }


    private RestTemplate getOrCreateRestTemplate() {
        if (restTemplate == null) {
            restTemplate = restTemplateBuilder.build();
        }
        return restTemplate;
    }

    @Override
    public void createMeeting() {

        String requestJson = "{\n" +
                "    \"subject\": \"Someone is in reception\",\n" +
                "    \"body\": {\n" +
                "        \"contentType\": \"HTML\",\n" +
                "        \"content\": \"Does noon work for you?\"\n" +
                "    },\n" +
                "    \"start\": {\n" +
                "        \"dateTime\": \"2021-10-21T16:00:00\",\n" +
                "        \"timeZone\": \"Australia/Brisbane\"\n" +
                "    },\n" +
                "    \"end\": {\n" +
                "        \"dateTime\": \"2021-10-21T18:00:00\",\n" +
                "        \"timeZone\": \"Australia/Brisbane\"\n" +
                "    },\n" +
                "    \"location\": {\n" +
                "        \"displayName\": \"Reception 1\"\n" +
                "    },\n" +
                "    \"attendees\": [\n" +
                "        \n" +
                "    ],\n" +
                "    \"allowNewTimeProposals\": false,\n" +
                "    \"isOnlineMeeting\": true,\n" +
                "    \"onlineMeetingProvider\": \"teamsForBusiness\"\n" +
                "}";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization", "Bearer " + getToken());

        HttpEntity entity = new HttpEntity(requestJson,headers);
        Map answer = getOrCreateRestTemplate().postForObject("https://graph.microsoft.com/v1.0/users/e8bce04c-2881-423d-bd14-5af9d98f6332/events", entity, Map.class);
        System.out.println(answer);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy