Back

Step by Step Guide to Building a Microsoft To Do API Integration in PHP

Aug 1, 20246 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your productivity with the Microsoft To Do API? In this guide, we'll walk through integrating this powerful tool into your PHP project. Whether you're building a task management system or just want to sync your to-do lists, you're in the right place. Let's dive in!

Prerequisites

Before we get our hands dirty, make sure you've got:

  • A PHP environment (you're a pro, so I'm sure you've got this covered)
  • Composer for managing dependencies (because who wants to manage packages manually?)
  • A Microsoft Azure account (don't worry, it's free to start)

Setting up the project

Let's kick things off by setting up our project:

mkdir todo-api-integration cd todo-api-integration composer init

Now, let's add the dependencies we'll need:

composer require guzzlehttp/guzzle league/oauth2-client

Registering the application in Azure AD

Time to get our app registered with Azure:

  1. Head over to the Azure Portal
  2. Create a new app registration
  3. Set up the required permissions for Microsoft To Do
  4. Grab your client ID and secret (keep these safe!)

Authentication

Now for the fun part - authentication! We'll use OAuth 2.0:

<?php require 'vendor/autoload.php'; $provider = new \League\OAuth2\Client\Provider\GenericProvider([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'redirectUri' => 'YOUR_REDIRECT_URI', 'urlAuthorize' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize', 'urlAccessToken' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token', 'urlResourceOwnerDetails' => '', 'scopes' => 'Tasks.ReadWrite' ]); // Get the URL to request authorization $authorizationUrl = $provider->getAuthorizationUrl(); // Get the access token using the auth code $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]);

Making API requests

With our access token in hand, let's start making some requests:

$client = new \GuzzleHttp\Client(); // GET tasks $response = $client->request('GET', 'https://graph.microsoft.com/v1.0/me/todo/lists', [ 'headers' => [ 'Authorization' => 'Bearer ' . $accessToken->getToken(), 'Content-Type' => 'application/json' ] ]); // POST new task $response = $client->request('POST', 'https://graph.microsoft.com/v1.0/me/todo/lists/{list-id}/tasks', [ 'headers' => [ 'Authorization' => 'Bearer ' . $accessToken->getToken(), 'Content-Type' => 'application/json' ], 'json' => [ 'title' => 'New task' ] ]); // Similar structure for PATCH (update) and DELETE requests

Error handling and best practices

Always expect the unexpected:

try { // Your API request here } catch (\GuzzleHttp\Exception\ClientException $e) { $response = $e->getResponse(); $responseBodyAsString = $response->getBody()->getContents(); // Handle the error }

And don't forget about rate limiting - be a good API citizen!

Sample implementation

Here's a quick taste of what you can do:

function getTasks($accessToken) { $client = new \GuzzleHttp\Client(); $response = $client->request('GET', 'https://graph.microsoft.com/v1.0/me/todo/lists/{list-id}/tasks', [ 'headers' => [ 'Authorization' => 'Bearer ' . $accessToken->getToken(), 'Content-Type' => 'application/json' ] ]); return json_decode($response->getBody(), true); } $tasks = getTasks($accessToken); foreach ($tasks['value'] as $task) { echo $task['title'] . "\n"; }

Testing the integration

Time to put our code to the test! Fire up Postman or your favorite API testing tool and start making requests. Don't be discouraged if things don't work right away - debugging is half the fun (right?).

Conclusion

And there you have it! You've just built a Microsoft To Do API integration in PHP. Pretty cool, huh? From here, you could extend this to create a full-fledged task management system, or maybe integrate it with other productivity tools. The sky's the limit!

Remember, the best way to learn is by doing. So go forth and code! And if you get stuck, the developer community is always here to help. Happy coding!