This is a description of how to get data from the Microsoft graph to your Android device’s home screen using Tasker and KLWP.
- A Microsoft account – No account, no data to fetch either. Chances are you already have this if you have a live.com/hotmail.com account or XBox account. Or you might have a work/school account.
- An Android device
- Tasker – The power tool for automating your Android device.
- KLWP – Kustom Live wallpaper. If you’d rather make a widget, KWGT would be the equivalent.
What is Microsoft Graph
Microsoft Graph connects all your data within the Microsoft eco system, such as emails in Outlook, pages in OneNote, files in OneDrive, etc. These can be queried through a REST-like API with support for some advanced queries. To test what data you might find, use the Graph Explorer.
The Graph Explorer is also a great way to fine tune the queries you will be using later. It might for instance be a good idea for performance to only fetch the fields you will be using.
Setup Azure app id for your Tasker flow
For the authentication flow to work, you need a registered application ID to authenticate with MSGraph. This can be freely set up in Azure with a free Azure account. We need this even when using Tasker, as we’re technically scripting our own custom application in Tasker.
Step 0: Create Azure account
At the Azure portal sign in with your Microsoft account, or github account.
Step 1: Create an application
- Go to the application registration “blade”.
- Tap “New registration”.
- In the registration page enter the required data:
- Enter a name for your “app”. Anything goes, this is for your information only.
- Select “Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)”
- Add Tasker’s OAuth response endpoint as the Redirect URL. (Type Web)
- Click “Register”
Step 2: Get application ID and a secret key
The application ID is displayed on the App registration page after the application is registered. Take a copy of this.
To generate the secret, go to “Certificates and secrets”.
- Click “New client secret”
- Give your secret a name, set expiry time and create.
- Copy your newly generated secret. You will not be able to copy it later.
Step 3: Grant permissions
By now you have all you need to connect your “app”, but it will not be allowed to access anything. For that we need to grant it the right permissions.
- Go to “API permissions”
- Click “Add a permission”
- Select Microsoft Graph API
- Use “Delegated permissions”
- Select permissions you expect to need. For OneDrive, you’ll need
Files.Read, Outlook mail will require
- Click “Add permissions”
Word of warning: It’s a good idea to not grant more permissions than you expect to use. In particular, don’t give your app write permissions unless you’re making some serious Tasker tasks that will be writing things back.
Create Tasker task to fetch data
With the paperwork done, we can move on to the fun stuff.
Open Tasker and go to the Tasks tab, then tap the + circle down on the corner to create a new Task. Give it a name.
As the first step of the task, add an
HTTP Auth action. On the action page,
- Method: OAuth 2.0
- Client ID: The ID you got when you registered your app on Azure
- Client Secret: The secret you generated on Azure
- Endpoint to get code:
- Endpoint to get refresh token:
- Scopes: Add the scopes you need here. Same naming as used in the
permissions set earlier. For this sample we need at least the
User.Readscope. For re-authentication to work, we also need to add
- Force reauthentication: Should be off, enable if you have auth issues. This will cause the login prompt on every execution of your task.
Fetch sample data from MSGraph
To test the app authentication, download the basic profile information from
/me. Display using a basic text popup.
Add an action of type
HTTP Request. In the action page, enter
- Method: GET
Then add an action of type
Popup and enter
Now run your task. It should ask for login, and after that perform the request and show the result in a popup.
Bridge to KLWP
There are two options for passing the data to KLWP, pass the entire file (by reference), or parse it in Tasker and pass each piece of information directly.
Pass the JSON file
To pass the retrieved JSON to KLWP and let KLWP do the parsing of data, store
the fetched data in files and pass the file location. To do this, set the
File/Directory to save with output parameter in the
HTTP Request task, then
pass the filename to KLWP in a single variable.
The file can then be accessed in KLWP using the
wg(br(tasker, graphme), json, .displayName)
A caveat here might be that KLWP seems to be somewhat slow on parsing JSON, so
frequent updates might slow things down. For more complex data such as
Inbox/Files, you will need to invoke the
wg() for every piece of information
Pass single information pieces
To parse the data in Tasker, make sure the
HTTP Request does not write to
file, then add a
to parse the
http_data global variable like this:
Then pass the variable exported using
setLocal to KLWP.
Another issue is the quickly escalating number of variables needed to pass to KLWP.
- Graph queries may be refined to only include the actual data you need. Use the
$selectquery parameter for this. This will save on both bytes downloaded and JSON parsing time. See the Microsoft graph documentation for details.
- This may work for your work or school account as well, but that depends on the security settings configured. Chances are high that your administrator has blocked access from unauthorized applications.
Update 2020-02-21: offline_access
Apparently, in order to get a
refresh_token with the auth request, you need to
add the scope
offline_access to the list of scopes. Updated the steps above to