• Web Development
  • Video

How to get HLS and DASH streaming urls using Microsoft Graph API

March 09, 2022 • 2 min read

You can use HLS or DASH urls for adaptive bit rate streaming in video players

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.


Format for DASH or HLS

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.


1
https://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.


1
https://graph.microsoft.com/v1.0/drives/<driveId>/items/<itemId>/content?format=hls

Handling the Response

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.

Sample Code

1
const https = require("https");
2
const options = {
3
hostname: "graph.microsoft.com",
4
path: "/v1.0/drives/<driveId>/items/<itemId>/content?format=dash",
5
method: "GET",
6
headers: {
7
Authorization:
8
"Bearer <your graph access token>",
9
}
10
};
11
12
const req = https.request(options, (res) => {
13
console.log(`statusCode: ${res.statusCode}`);
14
// Contains the URL to the manifest file used by video players to stream the video
15
console.log(res.headers.location);
16
17
if (res.statusCode === 302) {
18
// Get the contents of the manifest file
19
https.get(res.headers.location, (resGet) => {
20
resGet.on("data", (d) => {
21
process.stdout.write(d);
22
});
23
});
24
}
25
res.on("data", (d) => {
26
process.stdout.write(d);
27
});
28
});
29
30
req.on("error", (error) => {
31
console.error(error);
32
});
33
34
req.end();

Running the sample code

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.

  1. Go to the Microsoft Graph Explorer
  2. Configure the permissions so Graph Explorer has read access to your files. You can do this by clicking the ... next to your name on the left or right hand side depending on your locale 😀
  3. Copy the graph access token from the Graph Explorer
  4. On your machine create a JavaScript file, you can call it graphAPIFormatContent.js.
  5. Paste the sample code above in it
  6. Update the code to have the appropriate drive id and item id
  7. Run the code with the following command from a terminal or command prompt window
1
node 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 🎉.