Python script to connect to Brightspace API
I just joined this community. I am asking this question for a colleague (supervisor) who is trying to connect to the Brightspace API using python. We tried the postman (I am new to all of this so pardon my ignorance) which works fine, but when we try to get a response using python script, we get an error.
Has anyone tried using python to connect to the API? Could you please share some insight on this?
Thank you,
Roopa
SUNY Empire State University.
التعليقات
-
I use it to download datasets from the datahub into a database. I started from the examples from here:
https://community.brightspace.com/s/article/Brightspace-Data-Sets-Headless-Non-Interactive-Client-Example
Take a close look at the trade_in_refresh_token and get_with_auth functions.
It uses the Requests library from here:
-
Thank you so much for sharing this information, Randall. The latest I heard from my colleague is this:
I used postman to get the token and then used the token in my python and I still get the forbidden message. I have another script that opens a browser programmatically and works but requires user interaction.
My supervisor thinks if it's actually an issue with the API configuration itself. I will post an update as soon as I hear more from him.
Thank you again.
-
@Randall.S.394 Thank you for sharing the KB article. I couldn't open the link you shared, but I searched on the website and found this link below; I am hoping it's the same one that you referred to. We were wondering if you could share your working scrip t ? (with no credential information listed). Only if you are able; if not, it's totally ok.
Sorry one more question from my colleague (supervisor):
I am curious where Randall got the refresh token that's in the config.json file.
We really appreciate all your help. I am also going to try and reach out to D2L tech support and see if I can get some assistance.
Thank you again for your time and help.
Roopa -
Hi Roopa,
You have probably reached a solution by now, but I wanted to share mine with code samples here.
1. initial_authorization.py: This needs to run manually and it is an initial call where the browser window will open and a user will need to login to. This also will store the authorization code in an HTML file on a webserver so it could be retrieved and used to get the access token. After the access token is retrieved, it's saved locally in a text file and it's used to make a sample API call. This example calls https://<brightsapce_server>/d2l/api/lp/1.47/dataExport/list
2. automated_token_refresh.py: This can be scheduled to run as long as the token doesn't expire. It reads the token from the local text file and makes the same API call in number 1 above.
3. You need to setup your own redirect URI to handle the call back and to catch the authorization code and store it so the Python script can use to get the token. I used the below PHP script and my redirect URI looked like https://<your_server>/callback.php:
<?php
if (isset($_GET['code'])) {
$auth_code = $_GET['code'];
// Create a simple HTML file to store just the authorization code
$file = 'auth_code.html';
// The HTML content only contains the authorization code, no extra tags
$html_content = $auth_code;
// Write the authorization code to the HTML file
file_put_contents($file, $html_content);
echo "Authorization Code stored successfully in auth_code.html.";
} else {
echo "Authorization code not found.";
}
?>