Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

Try to create a pseudo-API that can access Office365 without coding.

Office365 provides an API called Microsoft Graph API that allows you to access Office365.

However, it is a bit time-consuming, as you need to acquire the token yourself and learn how to use the API.

So, we will show you how to use Office365's Flow to create a non-coding Office365 API.

What is Office365 Flow?

Office365 Flow is like Microsoft's version of IFTTT. You can connect various services with GUI and create a task pipeline.

Example: Let's create an API to send email from Outlook.

Flow as

Create a new Flow from scratch by selecting "Flow" from the grid menu in the upper left corner of Office365.

Creating API Receivers

The first step is to set the trigger for the Flow execution. By setting "Access by HTTP" as the trigger, the API is created.

Create a trigger in [Built-in]-[Request]-[On receipt of HTTP request].

When the Flow is saved, a URL is issued and is listed in the "URL for HTTP POST" section.

When that URL is accessed, it will trigger Flow to run, and we will create an API with it.
パラメータの設定 Parameters can be passed in JSON at the time of the URL call, and what parameters are passed is defined in the "JSON schema of the request body".

In this case, we will send an email, so "to", "title", and "message" can be set.

Also, since anyone can hit the URL, we will also send a "token" so that we can check the value of the token and only send the email if it matches.

Press "Generate schema using sample payload" and enter a sample JSON to send like the following.

Then, Yoshinari will create a JSON schema for you.

token check

Make sure that the token is checked to prevent anyone from executing the API.

Add a [Control]-[Condition] action.

Click "Select Value" to select "token" from the list of JSON parameters registered earlier, and enter the correct token value for comparison.

Condition setting in

Condition set

Send Email

Register the [Office365 Outlook]-[Send Mail] action when the "Condition" you just set is "Yes".

Set the JSON parameters "to," "title," and "message" to "destination," "subject," and "body.

API Response Settings

Register a [Request]-[Response] action after the "Send Email" action.

The settings here are sent to the API caller as the API response. This time, we want to return JSON like an API, so we write the following JSON in the "body".

That is all.

The entire process is as follows

You may create a response in case the token is incorrect. If you do not specifically create one, Flow will return a 502 error instead.

When the Flow is saved, the URL is fixed, so access that URL with "POST" and send parameters in JSON to execute the Flow.

Example of call

import axios from 'axios';

(async ()=>{
    const url = 'Flowで生成されたURL';
    const json = {
        token: 'aaaa',
        to:'test@test.test.test.com',
        title:'テストメール',
        message:'テストメール本文'
    };

    const res = await axios.post(url, json); 
    
    // Response from API "{"result": "success", "to":...}" is displayed
    console.log(res.data);
})();

Applications & Extensions

In the example, it was an API that causes Office365 to take action, but the body of the response can also contain data obtained during the Flow pipeline execution process, so it is possible to create an API that retrieves data from Office365, such as "retrieve the latest unread emails.

The services that can be connected with Flow are not only Office365, as is typical of Microsoft these days, but also Slack, Dropbox, Google, and various other external services, so it is possible to create APIs to operate them.

It is also possible to tap URLs from within Flow to connect with services for which no connectors are provided.

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com