function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Paul Allsopp 3Paul Allsopp 3 

Outlook API integration using OAuth2

Hey guys/gals,

I am trying to integrate the Outlook API from MS into a calendar app I'm building in Lightning. I have tried the code below, which I wrote today, but keep getting an error back saying: Malformed JSON. If I don't use JSON and send the request directly as a POST string, I get an error back saying the grant_type parameter is missing...although clearly not.

Any thoughts?

public Map<String, String> getAccessToken(String code) {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        HTTPResponse res;
        Map<String, String> access_data = new Map<String, String>();
        access_data.put('grant_type', 'authorization_code');
        access_data.put('client_id', APP_ID);
        access_data.put('client_secret', APP_PW);
        access_data.put('code', code);
        access_data.put('redirect_uri', APP_REDIRECT_URI);
        String request_body = (String)JSON.serialize(access_data);
        req.setHeader('Content-Type', 'application/json');

        try {
            res = http.send(req);
            if (res.getStatusCode() == 400) {
                throw new NoDataFoundException('Bad Server Request'); 
        } catch (Exception e) {
            Map<String, String> failure_response = new Map<String, String>();
            failure_response.put('Response', res.getBody());
            failure_response.put('Request', request_body);
            return failure_response;
        Map<String, String> json_data = (Map<String, String>)JSON.deserialize(res.getBody(), Map<String, String>.class);
        return json_data;

Best Answer chosen by Paul Allsopp 3
Paul Allsopp 3Paul Allsopp 3
Solved it! req.setCompressed(true); was causing the issue, as well as a couple of other tweaks