Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status Code 422 when trying to attach a file to an outlook calendar event

I'm trying to attach a text file to an existing Event in Outlook using graph-js-sdk-web.js I believe I followed instructions exactly but I get a 422 response. How do I get it to attach a file?

I'm pretty sure my eventID is correct because I can create an event and get it by that id. I'm pretty sure that my file's post load is correct because I attached file myself and then got that event by its id expanding for attachments and it was identical OData type, name, and content.

So far I googled the response and everything I've seen is either it just works for people or they were off on their code compared to that example.

Here are permissions I'm requesting

openid profile User.Read MailboxSettings.Read Calendars.ReadWrite

This matches permissions granted to the registered app.

This is the client and file attachment code:

// Create a Graph client
var client = MicrosoftGraph.Client.init({
    authProvider: (done) => {
        // Just return the token
        done(null, sessionStorage.accessToken);
    }
});

client
    .api('/me/calendar/events/' + eventID + '/attachments')
    .post({
        attachment: {
            "@odata.type": "#microsoft.graph.fileAttachment",
            name: "helloworld.txt",
            contentBytes: "SGVsbG8gd29ybGQh"
        }
    }, (error, response) => {
        if (error)
            console.log(error);
        else {
            console.log(response);
        }
    });

that produced this request payload

{
  "attachment":{
    "@odata.type":"#microsoft.graph.fileAttachment",
    "name":"helloworld.txt",
    "contentBytes":"SGVsbG8gd29ybGQh"
  }
}

the response I get from this is

{
  "error": {
    "code": "UnprocessableType",
    "message": "Cannot process input of abstract type 'Microsoft.OutlookServices.Attachment'",
    "innerError": {
      "request-id": "0a81e9f9-ef64-4b5e-b854-65e24fb71cfb",
      "date": "2019-05-14T23:57:29"
    }
  }
}

I'm not seeing what does it need to process attachment. What I find odd is that its abstract base class, not the one I provided in the odata.type field, which could be nothing.

I opened graph explorer and even though they don't have a working sample for attaching to event I used a post with this payload and my url and got the exact same response 422. That tells me its not the js library its something off with the graph api itself, either setup is different from their documentation or we are missing some undocumented setup requirement.

like image 208
Dmitry Avatar asked Nov 07 '22 17:11

Dmitry


1 Answers

Thanks to jasonjon's help the problem is solved. In the referenced instructions there's a mismatch in the payload example and javascript code sample. I went with js sample and had parent node attachment in the payload. Turns out there is no parent node. The correct way to use that api is

client
    .api('/me/calendar/events/' + eventID + '/attachments')
    .post({
       "@odata.type": "#microsoft.graph.fileAttachment",
       name: "helloworld.txt",
       contentBytes: "SGVsbG8gd29ybGQh"
    }, (error, response) => {
        if (error)
            console.log(error);
        else {
            console.log(response);
        }
    });
like image 169
Dmitry Avatar answered Nov 15 '22 10:11

Dmitry