📜  ondedrive - Javascript (1)

📅  最后修改于: 2023-12-03 15:18:06.453000             🧑  作者: Mango

OneDrive - JavaScript

OneDrive is a cloud-based storage and file sharing service provided by Microsoft. The OneDrive API allows you to manage your OneDrive files and folders programmatically using various programming languages including JavaScript.

Getting started

To use the OneDrive API with JavaScript, you need to register your application with Microsoft and obtain an access token. You can use the Microsoft Graph API to register your application and obtain an access token.

Once you have obtained the access token, you can use it to make API requests to the OneDrive API.

API endpoints

Here are some of the API endpoints that you can use with the OneDrive API:

Get user details
fetch("https://graph.microsoft.com/v1.0/me", {
    headers: {
        Authorization: `Bearer ${accessToken}`,
    },
})
    .then((response) => response.json())
    .then((data) => console.log(data));
Get file metadata
fetch("https://graph.microsoft.com/v1.0/drive/root:/path/to/file", {
    headers: {
        Authorization: `Bearer ${accessToken}`,
    },
})
    .then((response) => response.json())
    .then((data) => console.log(data));
Upload file
const formData = new FormData();
formData.append("file", file);

fetch("https://graph.microsoft.com/v1.0/drive/root:/path/to/file:/content", {
    method: "PUT",
    headers: {
        Authorization: `Bearer ${accessToken}`,
    },
    body: formData,
})
    .then((response) => response.json())
    .then((data) => console.log(data));
Create folder
fetch("https://graph.microsoft.com/v1.0/drive/root/children", {
    method: "POST",
    headers: {
        Authorization: `Bearer ${accessToken}`,
        "Content-Type": "application/json",
    },
    body: JSON.stringify({
        name: folderName,
        folder: {},
    }),
})
    .then((response) => response.json())
    .then((data) => console.log(data));
Conclusion

In this article, we have seen how to use the OneDrive API with JavaScript to perform operations like uploading files and creating folders. The API endpoints can be combined to perform more complex tasks. You can check out the official documentation for more details on the OneDrive API.