March 09, 2022 • 2 min read
If you want to stream videos in your Microsoft OneDrive or SharePoint, you can use the Microsoft Graph API to get the streaming urls. I was able to find out about two different types of streaming protocols that are supported by the API. They are DASH and HLS. I'm not sure if there are other streaming formats supported by the Microsoft Graph API we will discuss, but if you find out, please let me know.
This solution works for both personal and Enterprise OneDrive SharePoint.
To get the URLs, you can call the content API and pass it the format query param. As you can see, this will allow you to get your content in a specific format. You can't convert to any content you want because the content you want to format to is based on the file you are already working with. For example, you won't get a successful response if you try to take a video and format it as a pdf.
1https://graph.microsoft.com/v1.0/drives/<driveId>/items/<itemId>/content?format=dash
If you want to get the HLS url for streaming, simply replace the format with HLS.
1https://graph.microsoft.com/v1.0/drives/<driveId>/items/<itemId>/content?format=hls
If the request to the API is successful, the response will contain a 302
status code. This means that the
resource or content you need for streaming, AKA the manifest files, are present, but in a different "Location".
The DASH or HLS url you need is the url of the Location header in the response
.
Below, I have provided some sample code to help make this more concrete.
1const https = require("https");2const options = {3hostname: "graph.microsoft.com",4path: "/v1.0/drives/<driveId>/items/<itemId>/content?format=dash",5method: "GET",6headers: {7Authorization:8"Bearer <your graph access token>",9}10};1112const req = https.request(options, (res) => {13console.log(`statusCode: ${res.statusCode}`);14// Contains the URL to the manifest file used by video players to stream the video15console.log(res.headers.location);1617if (res.statusCode === 302) {18// Get the contents of the manifest file19https.get(res.headers.location, (resGet) => {20resGet.on("data", (d) => {21process.stdout.write(d);22});23});24}25res.on("data", (d) => {26process.stdout.write(d);27});28});2930req.on("error", (error) => {31console.error(error);32});3334req.end();
If you have Node Js installed on your machine, you should be able to run this code and get a successful response. Here are the steps to get this code working.
...
next to your name on the left or right hand side depending on your locale 😀graphAPIFormatContent.js
.1node graphAPIFormatContent.js
Once the code runs, you should see the DASH or HLS url printed to the console, along with the contents of the file they point to 🎉.