|

|  How to Implement Microsoft Graph API to Access Microsoft Planner in Python

How to Implement Microsoft Graph API to Access Microsoft Planner in Python

October 31, 2024

Learn how to access Microsoft Planner using Microsoft Graph API in Python with our comprehensive guide. Boost your productivity with detailed steps and tips.

How to Implement Microsoft Graph API to Access Microsoft Planner in Python

 

Overview of Microsoft Graph API

 

  • The Microsoft Graph API provides a unified programmability model that you can use to access data from various Microsoft services, including Microsoft Planner, which is part of the Office 365 suite.
  • It allows you to interact with the Planner to manage tasks, plans, and users effectively.

 

Prerequisites

 

  • Ensure you have the Microsoft Azure AD application configured with the necessary permissions to access Microsoft Planner data using the Microsoft Graph API.
  • Install the `requests` module in Python, which will facilitate HTTP requests to the Graph API.

 

pip install requests

 

Obtain Access Token

 

  • To authenticate requests to Microsoft Graph, you first need to retrieve an access token. This is typically done through OAuth 2.0 authorization flows.
  • Use the `ADAL` or `MSAL` library to obtain the token programmatically in your Python application.

 

import msal

def get_access_token(client_id, client_secret, tenant_id):
    authority = f"https://login.microsoftonline.com/{tenant_id}"
    app = msal.ConfidentialClientApplication(
        client_id=client_id,
        client_credential=client_secret,
        authority=authority
    )
    result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
    return result.get("access_token")

# Replace these values with your app's credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
tenant_id = "YOUR_TENANT_ID"

access_token = get_access_token(client_id, client_secret, tenant_id)

 

Access Microsoft Planner Data

 

  • Once you have the access token, use it to make HTTP requests to the Microsoft Graph API endpoints specific to Microsoft Planner.
  • The following Python code demonstrates how to retrieve a list of plans for a specific group (team).

 

import requests

def get_plans(group_id, token):
    endpoint = f"https://graph.microsoft.com/v1.0/groups/{group_id}/planner/plans"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    response = requests.get(endpoint, headers=headers)
    return response.json()

group_id = "YOUR_GROUP_ID"
plans = get_plans(group_id, access_token)
print(plans)

 

Handle API Responses and Errors

 

  • Always check the response status code to ensure the request was successful. The code should be 200 for a successful GET request.
  • Handle errors gracefully, possibly by implementing retry logic or logging error information for debugging purposes.

 

if response.status_code == 200:
    data = response.json()
    # Process the planner data
else:
    print(f"Error {response.status_code}: {response.text}")

 

Create and Manage Tasks

 

  • To manipulate data such as creating tasks or updating details in Planner, use POST, PATCH, or PUT requests with appropriate JSON payloads to the API endpoints.
  • Here is a sample code snippet that demonstrates creating a new task in a plan.

 

def create_task(plan_id, bucket_id, title, token):
    endpoint = "https://graph.microsoft.com/v1.0/planner/tasks"
    payload = {
        "planId": plan_id,
        "bucketId": bucket_id,
        "title": title
    }
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    response = requests.post(endpoint, headers=headers, json=payload)
    return response.json()

plan_id = "YOUR_PLAN_ID"
bucket_id = "YOUR_BUCKET_ID"
task_title = "New Task Title"
new_task = create_task(plan_id, bucket_id, task_title, access_token)
print(new_task)

 

Extend Functionality

 

  • For more complex interactions, consider using the `msgraph-sdk-python` package for a more robust and modern approach to interact with Microsoft Graph. This SDK offers features like batching requests and handling API version changes.
  • Regularly consult the official Microsoft Graph API documentation as it is continually updated with new endpoints and capabilities.