This sample uses Microsoft Graph to read your user Teams presence status.
This is based on my PythonTeamsPresence
Authentication is handled via device flow authentication, the recommended approach for PiBusyLight. If you're looking for examples of how to work with Microsoft Graph from Python web applications, see Python authentication samples for Microsoft Graph.
Verify that you have the following prerequisites in place before installing the sample:
- Install Python from https://www.python.org/. You'll need Python 3.6 or later, primarily because of the use of f-strings &mdash change those to format strings if you need to use an earlier Python 3.x version. If your code base is running under Python 2.7, you may find it helpful to use the 3to2 tools to port the code to Python 2.7.
- The sample can be run on any operating system that supports Python 3.x, including recent versions of Windows, Linux, and Mac OS. In some cases, you may need to use a different command to launch Python — for example, some Linux distros reserve the command
python
for Python 2.7, so you need to usepython3
to launch an installed Python 3.x version. - This sample requires an Office 365 for business account.
- To register your application in the Azure Portal, you will need an Azure account associated with your Office 365 account. No charges will be incurred for registering your application in the Azure Portal, but you must have an account. If you don't have one, you can sign up for an Azure Free Account.
Follow these steps to install the sample code on your computer:
-
Clone the repo with this command:
git clone https://github.com/poiriersimon/PiBusyLight.git
-
Install Raspbian with Desktop on you Pi
-
In the root folder of your cloned repo, install the dependencies for the sample as listed in the
requirements.txt
file with this command:pip install -r requirements.txt
.
To run the sample, you will need to register an application and add the registered application's ID to the configuration information in the config.py file. Follow these steps to register and configure your application:
-
Navigate to the Azure portal > App registrations to register your app. Sign in using a work or school account, or a personal Microsoft account.
-
Select New registration.
-
When the Register an application page appears, set the values as follows:
- Set Name to
PiBusyLight
. - Set Supported account types to Accounts in any organizational directory and personal Microsoft accounts.
- Leave Redirect URI empty.
- Choose Register.
- Set Name to
-
On the PiBusyLight overview page, copy and save the value for the Application (client) ID. You'll need it later.
-
Select API permissions.
- Choose the Add a permission button and then make sure that the Microsoft APIs tab is selected.
- In the Commonly used Microsoft APIs section, select Microsoft Graph, and then select Delegated permissions.
- Use the Select permissions search box to search for the
Presence.Read
permission. - Select the checkbox for each permission as it appears.
NOTE: Permissions will not remain visible in the list as you select each one.
-
Go to the Authentication page.
- Check the box next to
https://login.microsoftonline.com/common/oauth2/nativeclient
. - Find the setting labeled Default client type and set it to
Yes
. - Select Save at the top of the page.
- Check the box next to
After registering your application, modify the config.py
file in the root folder of your cloned repo, and follow the instructions to enter your Client ID (the Application ID value you copied in Step 3 earlier). Save the change, and you're ready to run the sample.
Follow these steps to run the sample app:
-
Go to Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins and do the Step 1 and 2
-
Modify the
config.py
to put the right GPIO code based on your setup -
At the command prompt, run the command
python PiBusyLight.py
. You'll see a message telling you to open a page in your browser and enter a code. -
After entering the code at https://aka.ms/devicelogin, you'll be prompted to select an identity or enter an email address to identify yourself. The identity you use must be in the same organization/tenant where the application was registered. Sign in, and then you'll be asked to consent to the application's delegated permissions as shown below. Choose Accept.
-
After consenting to permissions, you'll see a message giving your Presence and the corresponding LED will light
To simplify the multiple run of the script, it store the Refresh Token in a file with the script, please threath this as a password.
Several helper functions in helpers.py provide simple wrappers for common Graph operations, and provide examples of how to make authenticated Graph requests via the methods of the session object. These helper functions can be used with any auth library — the only requirement is that the session object has a valid Graph access token stored in its Authorization
header.
In this sample, the session object sends the required Authorization
header (which contains the access token) as well as optional headers to identify the libraries used. These headers are set during the authentication process. In addition, you may want to create other headers for certain Graph calls. You can do this by passing a headers
dictionary to the Graph call, and this dictionary will be merged with the default headers on the session object. You can see an example of this technique in parameter for any of the send_mail
helper function, which adds a Content-Type
header as shown here.
Converts a relative path such as /me/presence
to a full URI based on the current RESOURCE and API_VERSION settings in config.py.
Obtain the Access Token by leveraging the Refresh Token is present and valide, else leverage the device_flow_session to get a new one
Turn all LED off, no need to pass argument
Send the True/False to the specified GPIO
If you never used your GPIO to control LED this will set them up.
These samples are open source, released under the MIT License. Issues (including feature requests and/or questions about this sample) and pull requests are welcome. If there's another Python sample you'd like to see for Microsoft Graph, we're interested in that feedback as well — please log an issue and let us know!
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.